Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Associative Array Trouble

by mt2k (Hermit)
on May 23, 2002 at 05:15 UTC ( [id://168657]=note: print w/replies, xml ) Need Help??


in reply to Associative Array Trouble

mmm... might I add one bug that might occur from this example? What happens if the person has a last name containing spaces? I know a couple of people on this planet with last names like that... So I made one small adjustment to your code. I used a split() with a limit of 2:

#!/usr/bin/perl -w use strict; my $i = 1; my %list; open TEST, 'test.txt' || die "Couldn't open it"; print "\nFile Opened\n\n\n"; while(<TEST>){ chomp; my ($f, $l) = split(' ', $_, 2); print "$i : $l\,$f\n"; $list{$i} = [$f,$l]; $i += 1; } foreach my $c (sort keys %list) { print "$list{$c}->[1],$list{$c}->[0]$/"; } close TEST; print "\n\n\nFile Closed\n";

Also, may I ask why $list{$c}->[1],$list{$c}->[0] and $list{$c}[1],$list{$c}[0] are not really the same thing?
Is it because without the dereferencing arrows, it is not "technically correct" code?
I have never used the arrows and my code has always worked fine!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found