Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Merging two @ARRAYS into a %HASH

by jamgold (Initiate)
on Feb 25, 2000 at 21:43 UTC ( [id://4402]=CUFP: print w/replies, xml ) Need Help??

Hashes are a wonderful thing. They can be used to access structures in a clean and neat way.

Imagine you have a function that returns an @ARRAY of values, like localtime or stat. Wouldn't it be nice to be able to access the elements returned by their name? Here's how I do it:

my @datekeys = ("seconds","minutes","hour","mday","month","year","wday +","yday","isdst"); my %localtime; @localtime{@datekeys} = localtime( time ); printf "It now is %s:%s and the %d day of the week\n" ,$localtime{"hour"} ,$localtime{"minutes"} ,$localtime{"wday"} ;

Replies are listed 'Best First'.
RE: Merging two @ARRAYS into a %HASH
by howard40 (Beadle) on Feb 27, 2000 at 08:08 UTC
    i like this example.... Reminds me of Turbo Pascal... the "fake" hashes that could be constructed using only arrays... heh...
RE: Merging two @ARRAYS into a %HASH
by btrott (Parson) on Feb 25, 2000 at 23:59 UTC
    This is nice in a general purpose sense.

    In the specific instances you mention, though, you might want to also check out Time::localtime (and Time::gmtime) and File::stat, which provide by-name interfaces to their respective built-ins.

RE: Merging two @ARRAYS into a %HASH
by BBQ (Curate) on Apr 22, 2000 at 09:08 UTC
    I didn't get it! :o(

    I always knew you could do $this, @this and %this (and even *this) within the same program...
    But what's this new @this{}?

    #!/home/bbq/bin/perl
    # Trust no1!
      It's a hash slice! Hash slices are wonderful things. Instead of accessing one key at a time, you can access a list of keys:
      my %foo = ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, ); print @foo{ 'one', 'two' }, "\n"; print @foo{ 'three', 'four' }, "\n";
      You can assign values to them too:
      my %foo = (); @foo{ 'one', 'two' } = (1, 2); @foo{ 'three', 'four' } = (3, 4); print keys %foo;
      Everything chromatic says, but also, I thought I'd add that hash slices are very useful for stuffing an array into a hash (as in the original example). For example, say that you had a list of elements and you wanted to check if certain items were in that list. The best way to do this is with a hash, generally, and it's very easy to set up such a hash for lookups:
      my @list = qw/foo bar baz/; my %hash; @hash{@list} = (); # now I can test if an element is in @list # by checking if the key exists in %hash if (exists $hash{"foo"}) { print "found foo!"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://4402]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found