Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Generate Array of Hashes WITHOUT References

by dsheroh (Monsignor)
on Sep 09, 2014 at 11:48 UTC ( [id://1099978]=note: print w/replies, xml ) Need Help??


in reply to Re: Generate Array of Hashes WITHOUT References
in thread Generate Array of Hashes WITHOUT References

Fascinating how most replyers misunderstood you're question, cause they read the title but not the code.
I fail to see how explanations that you can only store scalars (such as references, but not hashes) in an array imply a misunderstanding of the questions asked in the OP, which, for reference, are: "I'd like to know how to do that without using references?" and "How can I do the same code the same thing without using references?"

You need to write %rec=() because you don't want references.
...and that still won't work, because the code ultimately does a push @AoH, $rec; Changing $rec to %rec will just plain not work because, even without strict, $rec and %rec are two different variables with no direct connection between them, so it would just be pushing an undef into @AoH.

Of course, that could easily be fixed by changing the push to push @AoH, %rec;, which will work... but it will do something completely different than what the OP wants, as explained in b4swine's answer.

Guess you misunderstood the question because you started reading the code, then addressed the first problem you saw without bothering to read all the code.

The answer to the question actually asked, as previous posters have correctly understood, is "You can't do that without using references."

Replies are listed 'Best First'.
Re^3: Generate Array of Hashes WITHOUT References
by LanX (Saint) on Sep 09, 2014 at 12:01 UTC
    > I fail to see how explanations that you can only store scalars ..

    I read the question as:

    "How can I avoid the dereferencing in $rec->{$key}=$value " and not "How can I store non-ref hashes in arrays."

    He tried $rec{$key}=$value but failed b/c he used %rec now.

    > ...and that still won't work, because the code ultimately does a push @AoH, $rec;

    true, but this was already demonstrated by Hameed but w/o explanation ... an explanation I added, without testing the code.

    Telling the OP (like most did here) that he can ultimately only store refs doesn't answer his question how to avoid dereferencing BEFORE storing.

    Of course most of his errors would have easily be visible if he used strict and warnings .

    But - alas - he mentioned that he took the code from the perldocs which (like I checked) don't use these pragmas either. :-(

    Finally I posted from mobile, it's hard to give working code from Android.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 13:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found