Solved

Scalar to command, arrays in hashes?

Posted on 2000-03-01
6
199 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:Raydot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2574314
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


0
 
LVL 3

Accepted Solution

by:
RobWMartin earned 75 total points
ID: 2574325
I said use array references, but actually I used anonymous arrays.  Ultimately the same thing as far as the hash value is concerned.  This would be more of an example of an array reference:

@dogs=("rex","fido","king");

%masterList=(
   dog => \@dogs,
   ....
)

Otherwise, everything is the same.

0
 
LVL 16

Expert Comment

by:maneshr
ID: 2574329
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";
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 2574434
%masterList = map{$_,\@{$_}} @arrayLists;
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2577352
were you able to check the comments that were offered?

let us know :)
0
 
LVL 3

Author Comment

by:Raydot
ID: 2583879
You guys are brilliant.  While ozo and man went above and beyond, RobW did answer first.  Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question