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

Hi, I am having a few problems printing a call to a hash.
hash: %random_name = ( $bob => "something", etc );
I have array which stores names given from on screen prompt, then want to print the values for these names
foreach (@array) { $value = $random_name{$_}; print "value: $value\n"; };
Nothing is printed, if i replace $random_name{$_}; with $random_name{$bob}; it works fine? Thanks

Replies are listed 'Best First'.
Re: calling a hash
by blazar (Canon) on Oct 04, 2005 at 11:20 UTC
    General purpose hint that may help you a lot:
    use strict; use warnings;
    I have array which stores names given from on screen prompt, then want to print the values for these names
    <div mode="esp">
    This suggests me you may have simply forgotten to chomp your entries. Does this ring a bell?
    </div>
      foreach (@array) {chomp; print "\n $_\n"; #sucessful print $value = $glycan_type{"$_"}; print "value: $value\n"; #nothing returned };
        Are you the same AM that wrote the root node? If so, then what are you trying to say? That the problem appears not to be a spurious "\n"? Well, in any case the form of my reply should have implied that IMNSHO you didn't give us enough elements.

        So I suggest you should give us more info, possibly preparing a minimal example still exhibiting the problem. Chances are that while doing so you will find an answer yourself. Otherwise you will help us to help you, which is fine, isn't it?

        Incidentally, re

        $value = $glycan_type{"$_"};
        no need to put $_ in quotes.
Re: calling a hash
by cbrandtbuffalo (Deacon) on Oct 04, 2005 at 11:54 UTC
    I think they are suggesting you should verify $bob has something in it. If you don't put a value in $bob, it creates a hash entry with the empty string. If you want a hash entry with the string 'bob', don't put the $ in front of it.

    I think this would explain your results. Printing $random_name{$bob} is actually $random_name{''} and you are probably entering the string 'bob' at the prompt.

    Try this to verify what you have in %random_name:

    use Data::Dumper; print Dumper( \%random_name );
      no sorry not the person who posted earlier. but i have resolved the problem, removing the $ in the hash corrected it Thanks for all of your help
        Indeed this is a situation in which turning on warnings would have solved the problem for you:
        $ perl -we '%a=($bob => "foo")' Name "main::a" used only once: possible typo at -e line 1. Name "main::bob" used only once: possible typo at -e line 1. Use of uninitialized value in list assignment at -e line 1.
        See in particular the last line. But then if had also turned on strict
        $ perl -Mstrict -e 'my %a=($bob => "foo")' Global symbol "$bob" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.
        So, to put it briefly: do it!
Re: calling a hash
by Samy_rio (Vicar) on Oct 04, 2005 at 11:24 UTC

    Hai,In the "%random_name" hash, $bob is treated as a string if its not declare.>

    Try this,

    use strict; my $bob="hai"; my @array=('hai'); my %random_name = ( $bob => "something"); foreach (@array) { my $value = $random_name{$_}; print "value: $value\n"; };

    Regards,
    Velusamy R.

      Hai, In the "%random_name" hash, $bob is treated as a string if its not declare.
      really?
      $ perl -MO=Deparse -e '%a=($bob => "foo")' (%a) = ($bob, 'foo'); -e syntax OK
      or
      $ perl -MData::Dumper -e '%a=($bob => "foo"); print Dumper \%a' $VAR1 = { '' => 'foo' };