Here's a short program that reads data into a hash of lists, sorts the keys and displays output data - a simple Perl program showing powerful capabilities of the language.
while () {
my ($name,@temporary) = split;
$want{$name} = \@temporary;
}
foreach $person (sort keys %want) {
print $person," ",$want{$person}[0]," ",$#{$want{$person}}+1,"\n";
}
__DATA__
antonia Perl XML PHP Tcl/Tk MySQL
ethel MySQL Perl Tcl/Tk ASP
iris Perl MySQL Java Tcl/Tk
olivia MySQL Python ASP PHP
uva MySQL Perl Java PHP
adam Tcl/Tk Perl Python MySQL
ed Ruby Perl Java PHP
ivan Ruby Java Perl Tcl/Tk MySQL
orpheus MySQL Ruby Tcl/Tk XML
ulsyees Java PHP Perl
Results:
adam Tcl/Tk 4
antonia Perl 5
ed Ruby 4
ethel MySQL 4
iris Perl 4
ivan Ruby 5
olivia MySQL 4
orpheus MySQL 4
ulsyees Java 3
uva MySQL 4
A little demo I wrote today while talking about
more complex data structures.
Things always turn out so easy in Perl!
(written 2004-09-09, updated 2006-06-05)
Associated topics are indexed under
P217 - Perl - More than Simple Lists and Hashes! [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
[3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
[3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3105] Adventure with references to lists and lists of references - (2010-12-26)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[2877] Further more advanced Perl examples - (2010-07-19)
[2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
[2241] Perl references - $$var and \$var notations - (2009-06-15)
[1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
[293] Course follow-ups - (2005-04-27)
Some other Articles
Effective fundraisingNear and far securityCGI v mod_perlSerious subjectHash of lists in PerlDo languages change?A Thousand and four wordsTake the train to trainingA Change is as good as a restAn update for old friends