Entry
How do you input a key and value into hash from <STDIN> ?
Sep 4th, 2005 04:13
Jason Ross, FrosT Premiso,
a quick and dirty way to do it is like so :
my %hash = map { split } <>;
the sample script below shows that in action.
when run, it will sit there accepting input until it encounters EOF
(which is emulated via pressing CTRL-D on most systems). Once EOF is
reached, it prints all the key->pairs that were entered in. (it assumes
that the input is a single line containing a space delimited key->value
pair.)
I just verified it ran and did that. I did /not/ check for breakage upon
encountering a hash of a hash, or any other debugging.
Obviously, if you wanted to use this on a real world app of some kind,
you should do some kind of input validation and sanitizing if
appropriate ...
#!/usr/bin/perl
use strict;
my %hash = map { split } <>;
while( my($key, $value) = each(%hash) ){
print "$key => $value\n";
}
=[ jason ]=