Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Scalar result from array

by packetstormer (Monk)
on Dec 03, 2010 at 15:51 UTC ( [id://875207]=perlquestion: print w/replies, xml ) Need Help??

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 ================

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://875207]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found