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

i have written small code which is intended for iterate over the loop and populate the hash,code is as below

use Data::Dumper; for ($i=0;$i<5;$i++) { $x{$i}="x"; } print Dumper($x);

but its not populating the hash as $var1={1=>x, 2=>x, 3=>x, 4=>x} Can you guys help me in populating this hash. Any help will be much appreciated

asab

Replies are listed 'Best First'.
Re: Help needed in populating the hash
by GrandFather (Saint) on Oct 16, 2011 at 02:50 UTC

    Let's take that code, add strictures then make it a little more Perlish. First for strictures:

    use strict; use warnings; use Data::Dumper; for ($i=0;$i<5;$i++) { $x{$i}="x"; } print Dumper($x);

    which gives us a great crop of errors:

    Global symbol "$i" requires explicit package name at noname.pl line 5. Global symbol "$i" requires explicit package name at noname.pl line 5. Global symbol "$i" requires explicit package name at noname.pl line 5. Global symbol "%x" requires explicit package name at noname.pl line 6. Global symbol "$i" requires explicit package name at noname.pl line 6. Global symbol "$x" requires explicit package name at noname.pl line 10 +. Execution of noname.pl aborted due to compilation errors.

    Note that we have a bunch of errors grumbling about $i, one for %x and one for $x. Previous answers have addressed the %x/$x issue so let's fix the errors by declaring the variables we actually want and adopt roboticus's fix for the bogus $x:

    use strict; use warnings; use Data::Dumper; my %x; for (my $i = 0; $i < 5; $i++) { $x{$i} = "x"; } print Dumper (\%x);

    Now let's look at a few more Perlish versions of that code:

    #1/ Perl for loop: my %x; for my $i (0 .. 4) { $x{$i} = "x"; } #2/ For as a statement modifier $x{$_} = 'x' for 0 .. 4; #3/ map generating hash key/value pairs: %x = map {$_ => 'x'} 0 .. 4; #4/ hash slice and x operator: @x{0 .. 4} = ('x') x 5;

    These are (arguably) ranked in order of Perl experience required to understand them. As a beginner I'd highly recommend the Perl for loop. It's way better than the C for loop for a variety of reasons. The other versions are there as a teaser to show why Perl is known for TIMTOWTDI. They each have their place and illustrate important Perl constructs, but probably oughtn't be the first tool of choice until you fully understand what is going on with them.

    True laziness is hard work

      The last two were instructive. Thanks!

        Thanks a lot for letting me know how to de-reference the hash

Re: Help needed in populating the hash
by roboticus (Chancellor) on Oct 16, 2011 at 02:05 UTC

    asab:

    Remember that $x is a different variable than %x. Try dumping %x instead (but put a \ before it, otherwise the hash will expand to a list, and Dumper will treat it as 10 values to dump.

    Also, rather than the "C" style for loop, you might find it better to use a perl style loop:

    use Data::Dumper; for $i (0 .. 4) { $x{$i}="x"; } print Dumper(\%x);

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Help needed in populating the hash
by Khen1950fx (Canon) on Oct 17, 2011 at 08:25 UTC
    Using a hashref:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper::Concise;; my(%x) = (qw(1 2 3 4)); my $ref = \%x; my $i; for ($i = 1; $i<5; ++$i) { $x{$i} = 'x'; } print Dumper($ref);