Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: adding a new hash in an array

by matija (Priest)
on Jul 14, 2004 at 16:40 UTC ( #374345=note: print w/ replies, xml ) Need Help??


in reply to adding a new hash in an array

Ouch, your code is a MESS! The only thing that could make it look uglier is a couple of gotos.

Your basic problem is that you are running without use strict; with it, you would realize that the code

{ my %row; }
does not do what you think it does.

You need to put the my outside of the loop, and instead of simply assigning new values to the hash, you need to make sure each row gets it's own anonymous hash.

The way I usualy do it looks somewhat like this:

foreach (@rows) { push(@arr,{}); $cnt++; # .... code ... $arr[$cnt]{somestring}=$somevalue; }
Actually, I usualy fill in the values right when I allocate the anonymous hash, but that couldn't be translated into what you need as easily.


Comment on Re: adding a new hash in an array
Select or Download Code
Re^2: adding a new hash in an array
by popsin (Initiate) on Jul 14, 2004 at 20:34 UTC
    Thanks,

    This is the code that works:

    my @arr; my $loop_status; my $loop_name; MAINLOOP: foreach $line (@lines) { ($name, $value) = split(/=/, $line); chomp($name); chomp($value); if ($name eq "loop" and $value eq "start") { $loop_status = "start"; next MAINLOOP; } if ($name eq "loop_name") { $loop_name = $value; next MAINLOOP; } if ($name eq "loop" and $value eq "stop") { $loop_status = "stop"; $template->param(eval "$loop_name" => \@arr); next MAINLOOP; } if ($loop_status eq "start") { if ($name eq "row" and $value eq "start") { $cnt++; push(@arr,{}); next MAINLOOP; } $arr[$cnt]{eval "$name"} = $value; } else { $template->param($name, $value); } } print "Content-type: text/html\n\n"; print $template->output;

    I know my code is a mess :-(
    But it works fine now.
    I tried to put use strict but I got the whole bunch of error messages so I will skip it.

    Another question:
    Is there a standard way to store session variables?

      I tried to put use strict but I got the whole bunch of error messages so I will skip it.

      That's the whole point of use strict. Those error messages are telling you why your code is broken. You would never have run into the problem you just had if your code was strict-compliant.

      Is there a standard way to store session variables?

      Indeed. Check out CGI::Session.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-07-13 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls