Solved

Scalar to command, arrays in hashes?

Posted on 2000-03-01
6
193 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Expert Comment

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

Expert Comment

by:maneshr
Comment Utility
were you able to check the comments that were offered?

let us know :)
0
 
LVL 3

Author Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now