Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: arrays and hashes blended !

by jeffa (Bishop)
on Sep 29, 2005 at 13:32 UTC ( #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
Node Status?
node history
Node Type: note [id://496097]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-06-04 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (30 votes). Check out past polls.

    Notices?