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

Hey all, I feel silly for not being able to figure out what I'm doing wrong... but here it is... I've got this code and I'm trying to get all of the form values... specifically, the INPUT names and values. The problem is, when I run it, it gives me an array for the input values and I can't seem to get anything out of the array except for: ARRAY(0x1d9d144) Can someone explain to me, are the numbers a memory address? And... what can I do to get the values in the array?
#!/usr/bin/perl -w #use strict; use LWP::Simple; use WWW::Mechanize; use HTML::Form; my $ua = LWP::UserAgent->new; my $response = $ua->get("http://www.ncbi.nlm.nih.gov/sites/entrez? +term=rnr2&cmd=Search&db=nuccore&QueryKey=17"); my @forms = HTML::Form->parse($response); foreach $item(@forms){ #print $item."\n"; while( my ($k, $v) = each %$item ) { #print "key: $k, value: $v.\n"; if ($k eq "action"){ $action = $v; print "\n\n"."ACTION: ".$action."\n"; } if ($k eq "method"){ $method = $v; print "\n\n"."METHOD: ".$method."\n"; } if ($k eq "attr") { print "\n\n"."ATTRIBUTES"."\n"; while( my ($k, $v) = each %$v ) { print "key: $k, value: $v.\n"; } } if ($k eq "inputs"){ print "\n\n"."INPUTS"."\n"; @newarray = $v; foreach $thisItem(@newarray){ print $thisItem."\n"; } } } }
UPDATE - I changed @newarray = $v; to @newarray = @$v; and it works. Now I have a boatload of hashes to go through! Thanks to thezip for helping me out!

Replies are listed 'Best First'.
Re: Array Question
by citromatik (Curate) on Feb 12, 2008 at 17:30 UTC

    In the INPUTS section you wrote:

    @newarray = $v;

    $v contains a reference to an array, and with this assignment you are creating an array with just one element (a reference to another array). Try this instead:

    @newarray = @$v

    This way, you are assigning to @newarray the array that $v refers to

    Hope this helps

    citromatik

    Update::Also, $thisItem holds a reference to a hash, you can investigate the referenced hash with Data::Dumper:

    if ($k eq "inputs"){ print "\n\n"."INPUTS"."\n"; @newarray = @$v; foreach $thisItem(@newarray){ print Dumper $thisItem; { }
Re: Array Question
by wfsp (Abbot) on Feb 12, 2008 at 18:03 UTC
    Actually you have a boatload of HTML::Form::TextInput objects. :-)

    So

    foreach my $thisItem (@newarray){ print Dumper $thisItem; print qq{\n}; printf qq{type: %s\n}, $thisItem->type; exit; }
    produces (extract)
    INPUTS $VAR1 = bless( { '/' => '/', 'name' => 'EntrezSystem2.PEntrez.DbConnector.Db', 'readonly' => 1, 'sid' => '1', 'type' => 'hidden', 'value' => 'nuccore', 'value_name' => '' }, 'HTML::Form::TextInput' ); type: hidden
Re: Array Question
by toolic (Bishop) on Feb 12, 2008 at 17:26 UTC
    When you read the documentation for HTML::Form, what type of data structure does the parse method return? Is it an array, or is it a reference to an array, or is it some other data structure?

    From the vague description of your problem, I suspect it returns a reference to an array. You can get more information by using:

    use Data::Dumper; print Dumper(\@forms);
Re: Array Question
by Erez (Priest) on Feb 12, 2008 at 17:53 UTC

    Giving up on strict halfway, isn't good practice, and so is dereferencing your data using @$ and %$.
    This been said, stuff like ARRAY(0x1d9d144) and HASH(0x8862690) refers to un-dereferenced reference. In this view:

    if ($k eq "inputs"){ print "\n\nINPUTS\n"; foreach my $thisItem(@{$v}){ my %tempHash = %{$thisItem}; foreach (keys %tempHash) { print "$_ $tempHash{$_}\n"; } } }

    Read the perlref section of perldoc for more information. And, as suggested above, the actual documentation of what the modules you use actually return, and handle it accordingly.

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphores.