Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Using foreach to iterate through a hash in an array

by choroba (Cardinal)
on Aug 30, 2017 at 11:28 UTC ( [id://1198327]=note: print w/replies, xml ) Need Help??


in reply to Using foreach to iterate through a hash in an array

my @bla = (%hash, %hash2); doesn't create an array of hashes, it flattens the hashes to key - value pairs, so @bla in fact contains
"00:20:00", "a", "10:00:00", "b", "00:00:00", "a",
etc. (in no particular order).

To create an array of hashes, use references:

my @bla = (\%hash, \%hash2);

You can then iterate with dereference

for (keys %{ $bla[0] }) {

or, in recent Perls,

for (keys $bla[0]->%*)

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Using foreach to iterate through a hash in an array
by Anonymous Monk on Aug 30, 2017 at 11:36 UTC
    Oh ok thank you. It works and it seems as if I have skipped a few tutorial pages. I will have to take another look into it and thanks again.
      I have tried iterating with foreach and the hash itself

      foreach (keys $hash){
          print $_ . "\n";
      }

      So far so good ...

      Given the code you OPed, it's hard to imagine this would have done anything "good" unless  $hash.was actually a hash reference. If this was the case, please be aware of this from the latest Perl documentation:

      Starting with Perl 5.14, an experimental feature allowed keys to take a scalar expression. This experiment has been deemed unsuccessful, and was removed as of Perl 5.24.
      This experimental ex-feature was also associated with values, each, push, pop and friends.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198327]
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-04-26 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found