spacewarp has asked for the wisdom of the Perl Monks concerning the following question:

I know this is probably a very stupid question, but in 2.5 years of PERL programming, I've never come across it before, and can't find the answer in my books.

Can a hash key have spaces in it? ie, can you have $names{'Space Warp'} ?

Spacewarp

DISCLAIMER:
Use of this advanced computing technology does not imply an endorsement
of Western industrial civilization.

Replies are listed 'Best First'.
Re: basic hash question
by mr.nick (Chaplain) on Jun 22, 2001 at 22:58 UTC
    Sure! Did you try? :)
    $ 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 ...

Re: basic hash question
by arturo (Vicar) on Jun 22, 2001 at 23:02 UTC

    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"'
Re: basic hash question
by myocom (Deacon) on Jun 22, 2001 at 22:58 UTC

    Yes, most definitely. Did you try?

Re: basic hash question
by Beatnik (Parson) on Jun 23, 2001 at 12:43 UTC
    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.
Re: basic hash question
by spacewarp (Pilgrim) on Jun 22, 2001 at 23:09 UTC
    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.
Re: basic hash question
by PetaMem (Priest) on Jun 23, 2001 at 18:52 UTC
    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". ;-)

Re: basic hash question
by mr.dunstan (Monk) on Jun 23, 2001 at 02:25 UTC
    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

      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);