Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Sort lists/Data Structures

by McDarren (Abbot)
on Dec 05, 2008 at 20:25 UTC ( #728369=note: print w/ replies, xml ) Need Help??


in reply to Sort lists/Data Structures

What you are looking for is the sort function.

However, I think you are over complicating things somewhat.
Consider the following:

#!/usr/bin/perl use strict; use warnings; my %people; while (my $line = <DATA>) { chomp($line); my ($person, $occupation, $hours) = split /\s+/, $line; $people{$person}{occupation} = $occupation; $people{$person}{hours} = $hours; } for my $person (sort keys %people) { print "Name: $person\n"; print "Job: $people{$person}{occupation}\n"; print "Hours: $people{$person}{hours}\n"; print "------------------\n"; } __DATA__ Mike Plumber 80 Laura Programmer 60 Mark Sales 70 Jeremy Cook 65
Does this do what you want?

Cheers,
Darren :)


Comment on Re: Sort lists/Data Structures
Download Code
Replies are listed 'Best First'.
Re^2: Sort lists/Data Structures
by mmontemuro (Initiate) on Dec 05, 2008 at 21:19 UTC
    For a few elements, I agree with what you have. My actual program has about 60+ elements. It gets a bunch of fields from a database. So in this case, I felt that a data structure returning a reference is easier to work with. So your previous solution works just fine to display these records. Thanks again for your speedy response.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://728369]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2015-07-08 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls