Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Good Perl book?

by Athanasius (Archbishop)
on Feb 11, 2017 at 02:45 UTC ( [id://1181726]=note: print w/replies, xml ) Need Help??


in reply to Re: Good Perl book?
in thread Good Perl book?

This is actually pretty close to what you wanted. Un-comment use warnings and the resulting message will help to identify the problem:

Use of uninitialized value in concatenation (.) or string at first.pl +line 5.

The problem loop is:

foreach my $num (@arr) { print "Index is: $num. Value is: $arr[$num] "; }

and as the output shows, the loop value is always one greater than the index. That’s because arrays in Perl (as in C) are subscripted starting from zero, so $arr[1] is actually the second element. The loop is easily fixed:

foreach my $num (@arr) { print "Index is: $num. Value is: $arr[$num - 1] "; # Subtra +ct 1 from the index }

Also un-comment use strict, and you’ll see that the only error message pertains to this line:

for($num = 0; $num<= $#arr;$num++) {

Declare $num as a lexical variable:

for (my $num = 0; ...

and strict is happy.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^3: Good Perl book?
by Anonymous Monk on Feb 11, 2017 at 02:58 UTC
    Thank you for your patience and guidance. It was stupid of me to comment it out. Perl and perlmonks are awesome. Respect.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found