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

sub stockquotegetter { my @quotes; $html = get("http://www.iex.nl/Koersen/Aandelen.aspx") or die "Couldn't fetch the stock quotes"; foreach $aandeel (@_) { @url = split(/Realtime koers $aandeel/, $html); @url1 = split(/LastPrice"><span>/, $url[1]); @url2 = split(/</, $url1[1]); #$html =~ m{basevalues['11754LastPrice'] = ([\d,]+) ;}; #printf $_; #printf $sales_rank #$html =~ m{ '11754LastPrice'] = (.*?);}i; # extract destination + of link #$url = $1; printf "$aandeel $url2[0]\n"; push (@quotes,$url2[0]); } printf @quotes } printf &stockquotegetter(qw(Aegon Aperam ING ASML DSM Boskalis KPN Ran +dstad PostNL TomTom));
why do I get only one quote in @quotes? Thank you very much in advance

Replies are listed 'Best First'.
Re: list pushing
by 2teez (Vicar) on Aug 24, 2012 at 13:45 UTC
    hi,

    I rewrote your script altogther, changing all your wrongly use printf to print and then using "my" for all your arrays, using warnings and strict. I figured your get function was from LWP::Simple module.
    And I got output. See below:

    use strict; use warnings; use LWP::Simple; sub stockquotegetter { my @quotes; my $html = get("http://www.iex.nl/Koersen/Aandelen.aspx") or die "Couldn't fetch the stock quotes"; my(@url,@url1,@url2); foreach my $aandeel (@_) { @url = split( /Realtime koers $aandeel/, $html ); @url1 = split( /LastPrice"><span>/, $url[1] ); @url2 = split( /</, $url1[1] ); #$html =~ m{basevalues['11754LastPrice'] = ([\d,]+) ;}; #printf $_; #printf $sales_rank #$html =~ m{ '11754LastPrice'] = (.*?);}i; # extract destinati +on of link #$url = $1; print "$aandeel $url2[0]\n"; push( @quotes, $url2[0] ); } print @quotes; } print stockquotegetter( qw(Aegon Aperam ING ASML DSM Boskalis KPN Randstad PostNL TomTom)) +;
    OUTPUT Aegon 4,08 Aperam 11,04 ING 5,65 ASML 45,91 DSM 37,85 Boskalis 26,82 KPN 6,73 Randstad 25,76 PostNL 2,90 TomTom 3,47 4,0811,045,6545,9137,8526,826,7325,762,903,471

    Please, note that the reconstruction was based on several assumptions.
    This is not the best re-write, you could get though. There are still several things, you could optimise.
    Please, compare your code with mine and get the best of the two!
    Please, forgive my assumptions if they were wrong. I was only trying to help blindly!! :)

      thanks both you guys, you are very helpful:)

Re: list pushing
by toolic (Bishop) on Aug 24, 2012 at 13:33 UTC
    Are you sure you're only getting 1 quote? Some may be unprintable (undef). Add this code to the end of your sub (Tip #4 from the Basic debugging checklist):
    use Data::Dumper; print Dumper(\@quotes);

    What module is get from?