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

What am i doing wrong here?
#!/user/bin/perl -w use strict; use Getopt::Long; my $key; my $value; my %options; GetOptions ( \%options, 'names:s@', 'address:s@'); while ( ($key, $value) = each %options) { print "$key = $value\n"; }
./hashtest.pl -names Manny output is: names = ARRAY(0x81609ec)

Janitored by tye: Remove PRE tags

2006-07-21 Retitled by GrandFather, as per Monastery guidelines
Original title: 'sorry if this is a stupid question'

Replies are listed 'Best First'.
Re: trouble acessing array result from GetOptions
by GrandFather (Saint) on Jul 21, 2006 at 04:16 UTC

    Umm, using too much vertical white space? Not saying what you expected to see? Printing a reference rather than dereferencing it?

    If the latter is the problem then try this version (note the @$value in the print):

    use strict; use warnings; use Getopt::Long; my $key; my $value; my %options; GetOptions (\%options, 'names:s@', 'address:s@'); while (($key, $value) = each %options) { print "$key = @$value\n"; }

    Prints:

    names = Manny

    Or using -names Manny -names Meany -names Minnie on the command line it prints names = Manny Meany Minnie.


    DWIM is Perl's answer to Gödel
      I had figured it out but couldnt delete this post, but thanks for your input!!

        One tip that may help you in similar situations in the future. Whenever you find yourself printing a stringified reference when you expected something else, it's helpful to use Data::Dumper;, and to dump the variable in question into Dumper to see what you get. For example, if you added print Dumper \%options; to your script, you would get a good look at the data structure, and that could help you figure out how to find within it the data you're trying to retrieve.

        Data::Dumper is a great "I wonder what this structure looks like" tool.


        Dave

        Don't delete the post. Others may benefit from your question. Add an Update at the end if you figure it out or the nature of the problem changes, but don't remove or edit the original material in a way that invalidates answers.


        DWIM is Perl's answer to Gödel