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

I am having "newbie" trouble getting my head around scalar and list contexts. The code below is returning scalar and I was hoping it wouldn't. Is it obvious what I am doing wrong?
foreach $y(@shows_to_find) {$show_nzb_d = $nzb_url.$y.$amp.$cat_id.$amp.$api_username_url +.$api_username.$amp.$api_key_url.$api_key; @dl_nzb = push(@dl_nzb,$show_nzb_d); #Remove next 2 lines - just for testing print $dl_nzb[1]; print "$show_nzb_d"."<br />"; }
So when I print second element of the dl_nzb list I get a numeric value instead of the value.

Replies are listed 'Best First'.
Re: Scalar result from array
by choroba (Cardinal) on Dec 03, 2010 at 16:01 UTC
    See push - it returns the number of elements in the target array after the push. Use it without the = assignment.
      See - silly newbie error. Just using push then print the list now works as expected. Thanks!
Re: Scalar result from array
by Generoso (Prior) on Dec 03, 2010 at 16:33 UTC

    May be this is what you are trying to do?

    #!/usr/bin/perl -w # use strict; my @shows_to_find = <DATA>; print "Data input \n"; foreach (@shows_to_find){print;} print "\n", '-' x (60), "\n"; my @dl_nzb = (); my $nzb_url = 'url '; my $amp = ' amp '; my $cat_id = 'cat_id '; my $api_username_url = 'url2 '; my $api_username = 'user name '; my $api_key_url = 'url 3 '; my $api_key = 'key'; foreach my $y(@shows_to_find) { chomp($y); my $show_nzb_d = $nzb_url.$y.$amp.$cat_id.$amp.$api_username_u +rl.$api_username.$amp.$api_key_url.$api_key; push(@dl_nzb,$show_nzb_d); } foreach (@dl_nzb){print;print"\n";} print "\n", '-' x (60), "\n"; __DATA__ abc dft45.xml ert rt653.xml abc ert57.xml

    Result:

    perl "C:\Documents and Settings\16916\My Documents\new 4.pl" Process started >>> Data input abc dft45.xml ert rt653.xml abc ert57.xml ------------------------------------------------------------ url abc dft45.xml amp cat_id amp url2 user name amp url 3 key url ert rt653.xml amp cat_id amp url2 user name amp url 3 key url abc ert57.xml amp cat_id amp url2 user name amp url 3 key ------------------------------------------------------------ <<< Process finished. ================ READY ================