Home  |  Linux  | Mysql  | PHP  | XML
From:Jatin Davey Date:Fri Sep  3 05:36:23 2010
Subject:Re: Traversing Hash printing two times
Changed it to:

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

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

$months{english} = \@english;
$months{french} = \@french;

for (keys %months) {
print "Months in $_ : @{$months{$_}} \n";
}

and it worked fine.

Thanks Uri.

Thanks
Jatin

On 9/3/2010 10:47 AM, Uri Guttman wrote:
>>>>>> "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
>
Navigate in group perl.beginners at sever nntp.perl.org
Previous Next


Your recent visits
Re: Flip-flop conversations using Socket
Rendering data structures using CGI::Application
RE: Need help with Mail::Sender
Need help with Mail::Sender



  
© 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