CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Sorting an array

 



andy_wood
Novice

Aug 26, 2009, 5:28 AM

Post #1 of 7 (1077 views)
Sorting an array Can't Post

Hi, I have tried looking elsewhere to find an answer to this but I don't seemed to have much luck...

My problem is that I have an array of strings. Each string holds a number of data elements separated by a tab.

For example: group_id \t position \t name \t address :

("Group 1 \t 1 \t Joe \t some address 1", "Group 1 \t 3 \t Tim \t some address 3", "Group 1 \t 2 \t Dan \t some address 2")

What I want to do is order the array based on 'position' so that it is sorted numerically ascending so that if I was to print the array after it had been ordered I would want to see:

Group 1 \t 1 \t Joe \t some address 1

Group 1 \t 2 \t Joe \t some address 2

Group 1 \t 3 \t Joe \t some address 3

Can anyone help - or give me a reference to somewhere that might tell me the kind of thing I would need to do?

Thanks.


1arryb
User

Aug 26, 2009, 8:20 AM

Post #2 of 7 (1071 views)
Re: [andy_wood] Sorting an array [In reply to] Can't Post

Hi andy,

Try this:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my @data = (
"Group 1 \t 1 \t Joe \t some address 1",
"Group 1 \t 3 \t Tim \t some address 3",
"Group 1 \t 2 \t Dan \t some address 2");

# Transform each element of @data into an array reference and sort them on the 2nd field.
# This works because map returns a list whose elements are the last lvalue calculated inside the {} block
# (in this case, $r).
my @values = sort { $a->[1] <=> $b->[1] } map { my @a = split(/\t/, $_); my $r = \@a; } @data;

# Print the result.
map { print join("\t", @$_), "\n" } @values;


Output:

Code
Group 1          1       Joe     some address 1 
Group 1 2 Dan some address 2
Group 1 3 Tim some address 3


Regards,

Larry


(This post was edited by 1arryb on Aug 26, 2009, 8:42 AM)


KevinR
Veteran


Aug 26, 2009, 11:27 AM

Post #3 of 7 (1062 views)
Re: [1arryb] Sorting an array [In reply to] Can't Post

Larry,

Didn't you scold me recently for posting "clever" code that makes people hate perl? Wink

http://perlguru.com/gforum.cgi?post=40262;#40262
-------------------------------------------------


1arryb
User

Aug 26, 2009, 11:30 AM

Post #4 of 7 (1057 views)
Re: [KevinR] Sorting an array [In reply to] Can't Post

Kevin,

Touche. On the other hand, doing this the "plain and simple way" turns out to be neither plain nor simple, anyway. Smile

Larry


KevinR
Veteran


Aug 26, 2009, 11:38 AM

Post #5 of 7 (1055 views)
Re: [1arryb] Sorting an array [In reply to] Can't Post

hehehe... no prob Larry. Just pulling your leg. Cool
-------------------------------------------------


spider
User


Aug 27, 2009, 3:31 AM

Post #6 of 7 (1040 views)
Re: [andy_wood] Sorting an array [In reply to] Can't Post

I try not to use so "clever" code as the other guys. :-)
Maby I'm not that clever....
I like to use structure that I understand, and I'm a fan of hash.
I like to read my data into a logical structur, and that it can be used for whatever I like after.
Ex:

Code
#!/usr/bin/perl 
use strict ;
use warnings ;

my (@array) = ("Group 1 \t 1 \t Joe \t some address 1", "Group 1 \t 3 \t Tim \t some address 3", "Group 1 \t 2 \t Dan \t some address 2") ;
my(%data) ;
foreach (@array){
my($group_id,$possision,$name,$address) = split("\t",$_) ;
#Had to create a separat key, since I guess the key is the kombination of group and possision.
#Alternativly you could create one hash for all the groups, and a new hash for each possision in the group.
#The downside of that solution is that it will be more complex, but more generic.
my($key) = "$group_id:$possision" ;
$data{$key}{group_id} = $group_id ;
$data{$key}{possision} = $possision ;
$data{$key}{name} = $name ;
$data{$key}{address} = $address ;
}

foreach my $key (sort keys %data){
print "$data{$key}{group_id} $data{$key}{possision} $data{$key}{name} $data{$key}{address}\n" ;
}


This outputs:
Group 1 1 Joe some address 1
Group 1 2 Dan some address 2
Group 1 3 Tim some address 3


spider
User


Aug 27, 2009, 3:39 AM

Post #7 of 7 (1039 views)
Re: [andy_wood] Sorting an array [In reply to] Can't Post

This is the solution with a little more complex hash structure.
Larrys solution is probably better, but I like hash structrures...


Code
#!/usr/bin/perl 
use strict ;
use warnings ;

my (@array) = ("Group 1 \t 1 \t Joe \t some address 1", "Group 1 \t 3 \t Tim \t some address 3", "Group 1 \t 2 \t Dan \t some address 2") ;
my(%groups) ;
foreach (@array){
my($group_id,$possision,$name,$address) = split("\t",$_) ;
$groups{$group_id}{$possision}{group_id} = $group_id ;
$groups{$group_id}{$possision}{name} = $name ;
$groups{$group_id}{$possision}{address} = $address ;
}

foreach my $group_id (sort keys %groups){
foreach my $possision (sort keys %{$groups{$group_id}}){
print "$group_id $possision $groups{$group_id}{$possision}{name} $groups{$group_id}{$possision}{address}\n" ;
}
}


The outprint is the same.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives