Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Array reference of hash references

by PrincePerl (Novice)
on Oct 25, 2011 at 22:30 UTC ( [id://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

Replies are listed 'Best First'.
Re: Array reference of hash references
by toolic (Bishop) 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 (Cardinal) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found