Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: arrays and hashes blended !

by jeffa (Bishop)
on Sep 29, 2005 at 13:32 UTC ( [id://496097]=note: print w/replies, xml ) Need Help??


in reply to arrays and hashes blended !

My only disappointment is that this is not a Perl-ish recipe for a mixed drink. :(

My only suggestion is to avoid using 'hard coded' hash keys and array indices. I prefer to use a while loop to gather STDIN instead of a for loop, but there are many, many different ways to do this. Here's my take on it:

use strict; use warnings; my %hash; my @array; my $key = 'aa'; my $prompt = "\nEnter value <cntl-d exits>: "; print $prompt; while (<STDIN>) { chomp; push @array, $_; print $prompt; } $hash{$key++} = $_ for @array; print "\n"; print "$_ contains $hash{$_}\n" for sort keys %hash;
Feel free to ask any questions you may have about this code. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found