$ perl -de 1
main::(-e:1): 1
DB<1> $hash{"foo bar"}=1
DB<2> print keys %hash
foo bar
DB<3>
But you, of course, can't do $hash{FOO BAR} (without the quotes).
mr.nick ...
| [reply] [d/l] [select] |
Questions like this are best answered by *trying it out*, don't you think?
Of course it's kosher. The only issue is that you won't be able to make use of the autoquoting mechanism for hash keys.
perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>);
+$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth
+er_name\n"'
| [reply] [d/l] |
Yes, most definitely. Did you try?
| [reply] |
Yes, you can... Basically if you have spaces if your hash key, you have to quote it. Hash keys without spaces can be left unquoted (altho I'm not sure how this will turn in Perl6).
$hash{Foo}; #No quotes needed
$hash{Bar};
$hash{'Foo Bar'}; #Spaces, so quotes
Either you just read the wrong books, or you just read wrong.Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur. | [reply] [d/l] |
You know, one of my biggest problems in life is that I often don't think of the most obvious solutions to problems.
Spacewarp
DISCLAIMER:
Use of this advanced computing technology does not imply an endorsement
of Western industrial civilization.
| [reply] |
You can - of course. Should have tried out, shouldnīt you?
In fact you can give almost any string as a key to a hash:
quotation is very important then...
#!/usr/bin/perl -w
my %hash;
my $var = "Oh";
$hash{'$var'} = 'Whatever';
$hash{"$var"} = 'Whatīs that';
$hash{$hash{"$var"}} = '? I really donīt have a clue!';
print "$var\n";
print "$var - $hash{$var}\n";
print "$hash{'$var'}\n";
This gives you the following output:
Oh
Oh - Whatīs that
Whatever
Question: What output gives you (if appended
to the code above):
print $hash{"$var"}.$hash{$hash{"$var"}}."\n";
Ciao
Update: Of course $hash{"$blah"} is better off when
written $hash{$blah} see the interpolating quotation as
"educational obfuscation". ;-) | [reply] [d/l] [select] |
Not a dis at all, but isn't putting spaces into the names of hashes in general kind of a 'bad thing?'
You can do it, but to use spaces as keys is kind of wrong, right?!
mr.dunstan | [reply] |
No, it's not bad at all, as long as you remember that you need to quote those keys later (i.e., $hash{'two words'} works, where $hash{two words} does not).
As a somewhat overly contrived example, say you have a file with the rosters of each MLB team. Further, let's say it contains rosters for the last five seasons. Obviously many players are going to show up more than once. So how do you find out how many unique players there were over the last five years? Read the file and use the names as hash keys.
# Untested and unoptimized code
use strict;
my $filename = 'mlbroster.txt';
my %seen;
open (FILE, $filename) or die "Couldn't open $filename: $!\n";
while (<FILE>) {
chomp;
$seen{$_}=1; # This will include, say, $seen{'Sammy Sosa'}
}
close(FILE);
print "Unique players:\n", join("\n", sort keys %seen);
| [reply] [d/l] [select] |