Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting on Keys

Status
Not open for further replies.

Porshe

Programmer
Jun 5, 2001
38
US
HI everyone...

I have a data file with about 9 fields in it. How can I make one field a key then get all the information about that record (the other 8 fields) in that record? I have to do a sort on one field then get all the other information from the remaining fields.

Thanks,
Porshe
 
You can build a hash using the one field as the key value, and either a string concatenating all the other fields as the value, or an anonymous array reference as the value. Then to sort by the keys just use:
Code:
foreach my $key (sort keys %hash) {
   #do something with the values here
}
Sorry if this answer is somewhat generic. If you can tell us what the data records look like, and what you need to do with them, we can be more help. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
find a way of breaking (or accessing) your fields
individually. Then sort, search, whatever...

unix /etc/passwd contains 7 fields separated by ":"
to sort by 3rd field (user id):

perl -e '@a=<>; @b = sort {
@aa=split /:/,$a;
@bb=split /:/,$b;
$aa[2] <=> $bb[2] } @a; print @b' < /etc/passwd

to access information for uid == 0 (root):

perl -e 'while (<>) {
@aa = split /:/;
$aa[2] == 0 or next;
print; ## fields on @aa, original record on $_
last }' < /etc/passwd


regards

--
pkiller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top