Home  |  Linux  | Mysql  | PHP  | XML
From:Uri Guttman Date:Fri Sep  3 05:17:03 2010
Subject:Re: Traversing Hash printing two times
>>>>> "JD" == Jatin Davey <jashokda@cisco.com> writes:

JD> #!/usr/bin/perl
JD> use warnings;
JD> use strict;

very good to see those.

JD> my @english = qw(january february march april may june july);
JD> my @french = qw(janvier fverier mars avril mai juin juily);

JD> my %months;
JD> my $eng_ref;
JD> my $fre_ref;

JD> $eng_ref = \@english;
JD> $fre_ref = \@french;

no need for that. you can assign the refs directly into the hash.

JD> $months{english} = $eng_ref;
JD> $months{french} = $fre_ref;

JD> for (keys %months) {

that is assigning each key to $_. you never use $_. so this will loop
TWO times as there are two keys.

JD> print "Months in english : @{$months{english}} \n";
JD> print "Months in french : @{$months{french}} \n";

so both lines get printed twice.

what you want is more likely this:

foreach my $month (keys %months) {

print "Months in $month : @{$months{$month}}\n";
}

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
Navigate in group perl.beginners at sever nntp.perl.org
Previous Next


Your recent visits
Re: Traversing Hash printing two times
Re: Traversing Hash printing two times
Re: Flip-flop conversations using Socket
Rendering data structures using CGI::Application



  
© No Copyright
You are free to use Anything, but please consult your advocate before doing so as this website
also list content from other sources which may be copyrighted.
Site Maintained by Zareef Ahmed
Powered By PHP Consultants