http://www.perlmonks.org?node_id=452543

Madam has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks, In a 'foreach' loop ,i am appending the hash.but it is taking the last values,the intial elements are not getting added. i.e,
my @array = ("white","blue","yellow"); my %hash = (); foreach my $temp (@array) { %hash = ($temp => $temp); } print keys %hash;
only "yellow" is getting printed. where am i going wrong? Thanks, Madam

Replies are listed 'Best First'.
Re: Hash not getting updated
by blazar (Canon) on Apr 29, 2005 at 08:21 UTC
    %hash = ($temp => $temp);
    $hash{$temp}=$temp;
Re: Hash not getting updated
by davidj (Priest) on Apr 29, 2005 at 08:30 UTC
    blazar is right. That is the way you need to do it. The reason %hash had only one element in your original loop is because with each iteration of the loop it was being recreated: %hash = ($temp => $temp) .

    davidj
Re: Hash not getting updated
by monkfan (Curate) on Apr 29, 2005 at 13:52 UTC
    Hi,

    Is this what you want?
    #!/usr/bin/perl -w use strict; use Data::Dumper; my @array = ("white","blue","yellow"); my %hash = (); foreach my $temp (@array) { $hash{$temp} = $temp; } print Dumper \%hash; print join (" ",keys %hash);
    That gives:
    $VAR1 = { 'white' => 'white', 'blue' => 'blue', 'yellow' => 'yellow' }; white blue yellow
    If you want to learn more about hash or list, check this out,it gives solid rudimentary concept.
    When I first learn Perl, I also found this information about constructing data structure in Perl very useful.

    Regards,
    Edward
Re: Hash not getting updated
by CharlesClarkson (Curate) on Apr 30, 2005 at 07:04 UTC

    I assume this is just contrived code to ask a question, but, just in case, a hash slice makes the 'foreach' block unnecessary.

    my @array = ( 'white', 'blue', 'yellow' ); my %hash; @hash{ @array } = @array;

    Or, without the array:

    my %hash; @hash{ 'white', 'blue', 'yellow' } = ( 'white', 'blue', 'yellow' );
      I had an issue in my code,that's why i had asked this question and given a small example which was very close to the issue i was facing.And your reply helped me to solve the issue. i feel instead of @hash{ @array } = @array; $hash{@array} = @array ; would be better

        Have you tried it like that?

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper 'Dumper'; my @colors = qw( white yellow green ); my %hash; $hash{ @colors } = @colors; print Dumper \%hash; __END__

        I get this in perl 5.8.6:

        $VAR1 = { '3' => 3 };

        Perl assumes you want to evaluate the array in scalar context. Better to stick to the same notation everyone else uses for hash slices.

        HTH,
        Charles
Re: Hash not getting updated
by gube (Parson) on Apr 30, 2005 at 01:22 UTC

    Hi, try this here is the problem and solution,

    The problem is your key and value is assigned to hash. Each time they assigned. so, in that final value only stored in hash and the key printed for that hash is yellow. ie. That is the problem.

    So, don't assigned to hash each time, please use like below mentioned code.

    my @array = ("white","blue","yellow"); my %hash = (); foreach my $temp (@array) { $hash{$temp}=$temp; } print keys %hash;

    Regards
    Gube.