Solved

Scalar to command, arrays in hashes?

Posted on 2000-03-01
6
197 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
Technology Partners: 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
PERL export multiple query results to a JSON file 1 178
stftime format 4 58
syslog unix file 20 80
Vb script to unzip a files and rename the files 5 104
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

730 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