Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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?

Darren :)

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://728369]
[Happy-the-monk]: marto: like not coming back to it until the following day would be my advise to myself.
[marto]: Happy-the-monk very wise

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-03-18 18:08 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (230 votes). Check out past polls.