Link to home
Start Free TrialLog in
Avatar of Raydot
Raydot

asked on

Scalar to command, arrays in hashes?

I want to store a bunch of arrays, and then store a single array list with all of the other arrays inside.  So:

@arrayLists = ("dog","cat","rat");
@dog = ("fido", "rex", "king");
@cat = ("tabby", "Kitty", "Morris");
@rat = ("Ratbert", "Sam", "Rudy");

Is there a way to retrieve the values from dog, cat, and rat, by using the values in arraylists?

Also, could I store this all in a single hash?

%masterList = (dog => @dog, cat => ("tabby","rex","king") ...etc for rat)

How would I retrieve these values?

There's gotta be a way...

Raydot.
Avatar of RobWMartin
RobWMartin

You need to use array references in the case of the hash (which, by the way, seems like the best structure to use):


%masterList=(
  dog => ["fido","rex","king"],
  cat => ["tabby","fluffy","princess"],
);

print "My third dog is $masterList{'dog'}[2]\n";

would print

My third dog is king


ASKER CERTIFIED SOLUTION
Avatar of RobWMartin
RobWMartin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this. with this you dont even need the masterlist hash!!

#!/usr/local/bin/perl

@arrayLists = ("dog","cat","rat");
@dog = ("fido", "rex", "king");
@cat = ("tabby", "Kitty", "Morris");
@rat = ("Ratbert", "Sam", "Rudy");

#%masterList = (dog => @dog, cat => @cat rat => @rat)

foreach $arr (@arrayLists){
  foreach(@$arr){
    print $_,"\t";
  }
  print "\n";
}
Avatar of ozo
%masterList = map{$_,\@{$_}} @arrayLists;
were you able to check the comments that were offered?

let us know :)
Avatar of Raydot

ASKER

You guys are brilliant.  While ozo and man went above and beyond, RobW did answer first.  Thanks!