Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Array reference of hash references

by PrincePerl (Novice)
on Oct 25, 2011 at 22:30 UTC ( #933721=perlquestion: print w/ replies, xml ) Need Help??
PrincePerl has asked for the wisdom of the Perl Monks concerning the following question:

Hello, still new to perl and learning. I am dealing with references and

i wanted to have an Array reference of hash reference, but i keep getting nothing, but output like this: $VAR1 = [] when i use Data::Dumper

help

while(<INFILE>) { @off = split(/' '/,$_); %find = ("start" => $off[0], "stop" => $off[1]); my $hash_ref = \%find; my @official = []; push @official, $hash_ref; }

what am i doing wrong smart people.

Example file

1234 2345

1234 2345

3345 4456

Comment on Array reference of hash references
Download Code
Re: Array reference of hash references
by toolic (Chancellor) on Oct 25, 2011 at 22:40 UTC
    You didn't show where you called Dumper, and you didn't show what variable you are dumping. If you called it outside the while loop, you likely have a problem with variable scoping:
    use warnings; #use strict; use Data::Dumper; while (<DATA>) { @off = split( /' '/, $_ ); %find = ( "start" => $off[0], "stop" => $off[1] ); my $hash_ref = \%find; my @official = []; push @official, $hash_ref; print Dumper(\@official); } __DATA__ 1234 2345 1234 2345 3345 4456
    prints...
    $VAR1 = [ [], { 'stop' => undef, 'start' => '1234 2345 ' } ]; $VAR1 = [ [], { 'stop' => undef, 'start' => '1234 2345 ' } ]; $VAR1 = [ [], { 'stop' => undef, 'start' => '3345 4456 ' } ];

      i see! thanks, i was using dumper outside of the scope

      I wanted "start" to have the first number and "start" and it looks like its undefined.. Sighs

      why would it be undefined? I appreciate all the help you can give.

        I wanted "start" to have the first number and "start" and it looks like its undefined.. Sighs why would it be undefined?
        Because you are trying to split on single quotes surrounding a single space, but you have no single quotes in your data. Just use this:
        while (<DATA>) { my @off = split;
Re: Array reference of hash references
by choroba (Abbot) on Oct 25, 2011 at 22:41 UTC
    I see several problems:
    1. You assign a reference to an empty anonymous array as the first and only element of @official. Do not you rather want to assign an empty array? If yes, change [] to ().
    2. You define @official with my. If you are then dumping it from outside the loop, it does not exist anymore. Are you using strict?
    3. Even if you broaden the scope of @official, it will not contain all the results, because you assign the reference to it (see 1).
Re: Array reference of hash references
by Util (Priest) on Oct 25, 2011 at 22:45 UTC
    Initialize an array with parens (or not at all), *not* with square brackets.
    perl -MData::Dumper -wle 'my @official = []; print Dumper \@official;' $VAR1 = [ [] ]; perl -MData::Dumper -wle 'my @official = (); print Dumper \@official;' $VAR1 = [];
    Also, declare @official outside the while loop. Otherwise, you are re-initializing it on each loop.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-08-21 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (139 votes), past polls