Solved

Scalar to command, arrays in hashes?

Posted on 2000-03-01
6
196 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 159
rename outfile before writing 2 74
Question about @INC variable in perl 1 65
pipe to sed or perl, please help 5 130
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

828 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