
spider
User

Aug 27, 2009, 3:31 AM
Post #6 of 7
(1867 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:
#!/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
|