Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Array of hashes of arrays

by LeGo (Chaplain)
on Nov 10, 2000 at 09:49 UTC ( [id://40900]=perlquestion: print w/replies, xml ) Need Help??

LeGo has asked for the wisdom of the Perl Monks concerning the following question:

I am having a bit of a problem with referencing things.

I have an array that is full of hashes. Those hashes have arrays in them.

I would like to reference that last array. I can reference the individual scalars in those arrays with the following.

print "$mass_info[0]{cpu_bursts}[0]";
Here I am referencing the 1st hash, then the cpu_burst key and thats first element. I would like to reach that whole array at once though. I would like to be able to pop, shift, run foreach loops, join, and just lots of fun things with that array but don't know how to reference the whole array at once. I have tried many things and been unsuccesful.

Help with this is very much appreciated.

LeGo

Replies are listed 'Best First'.
Re: Array of hashes of arrays
by extremely (Priest) on Nov 10, 2000 at 10:13 UTC
    Oh  @{$mass_info[0]{cpu_bursts}} should do yah fine...
    push @{$mass_info[0]{cpu_bursts}}, $value; join ", ", @{$mass_info[0]{cpu_bursts}}; # etc...

    --
    $you = new YOU;
    honk() if $you->love(perl)

(jcwren) RE: Array of hashes of arrays
by jcwren (Prior) on Nov 10, 2000 at 10:15 UTC
    Here's an example of creating a data structure similiar to what you're talking about, and popping and pushing elements into it.

    I also tend to prefer the arrow notation, rather than the implicit mode. I believe the arrow notation makes it more clear. But that may be my 'C' background talking.

    Update: Looks like I was too slow. Oh well.
    #!/usr/local/bin/perl -w use strict; use Data::Dumper; { my @arr = ({-a => [qw(a1 a2 a3)]}, {-b => [qw(b1 b2 b3)]}, {-c => [ +qw(c1 c2 c3)]}); print "Dumping initial data structure -->\n"; print Dumper ([\@arr]), "\n"; print "Displaying elements of @arr [1]->{-b} -->\n"; print "$_\n" foreach (@{$arr [1]->{-b}}); print "\n"; pop (@{$arr [2]->{-c}}); print "Dumping data structure with @arr[2]->{-c} popped (no c3) --> +\n"; print Dumper ([\@arr]); push @{$arr [0]->{-a}}, "a4"; print "Dumping data structure with a4 added to @arr[0]->{-a} -->\n" +; print Dumper ([\@arr]); }
    --Chris

    e-mail jcwren
(tye)Re: Array of hashes of arrays
by tye (Sage) on Nov 10, 2000 at 10:13 UTC

    You use @{ }:

    push @{ $mass_info[0]{cpu_bursts} }, 14;

    I'd like to be able to use:

    push $mass_info[0]->{cpu_bursts}->@, 14;
    but that hasn't been implemented yet.

            - tye (but my friends call me "Tye")
      That is evil notation tye. Is that going to be implemented or are you jes' wishin'?

      Everything in perl up till now has been to prefix for context. If you want a scalar, you start with a '$', if you want an array you use an '@' and so on.

      --
      $you = new YOU;
      honk() if $you->love(perl)

      Actually it has been implemented already. And discussed.

      Perhaps you think it needs to be discussed again? :-)

        Well, for a minimalist's version of "implemented". For those that don't take the time to read the referenced thread from the p5p e-mail list. It appears that a patch to implement it has been created but not been accepted to any current or future version of Perl, at least partially because the next version of Perl was in a "feature freeze" at the time.

        It is unclear to me whether the patch was fully analyzed nor whether it would have been accepted had there not been a "feature freeze". So whether this will be in a future version of Perl is still in doubt (at least to me).

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-20 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found