in reply to Associative Array Trouble
Update: You have a terrible error that's one of my hobbyhorses, and I missed it on the first cut :( $a is sacred to sort
You're assigning a whole new %list each time around.
Also, when you want the values, youwhile(<TEST>){ chomp; /(\w+)\s+(\w+)/; print "$i : $2\,$1\n"; $list{$i} = [$1,$2]; $i+=1; }
That should fix things up.foreach my $c (sort keys %list) { print "$list{$c}->[1],$list{$c}->[0]$/" }
Update: Softened statement on dereferencing and corrected horrible error mixing $a as temporary with sort.
Update 2: Here's a line to sort and print all at once
You might also consider split instead of re+backreferences for parsing your data, more efficient. Edit, corrected typo of dropped parenprint map {"$_->[1], $_->[0]$/"} sort {lc($a->[1]) cmp lc($b->[1])} va +lues %list;
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Associative Array Trouble
by EmmittSmith (Acolyte) on May 23, 2002 at 00:05 UTC | |
by tadman (Prior) on May 23, 2002 at 02:18 UTC |