Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Good Perl book?

by Anonymous Monk
on Feb 10, 2017 at 15:56 UTC ( [id://1181685]=note: print w/replies, xml ) Need Help??


in reply to Good Perl book?

Hi all

Thank you for your responses. I just tried the following. Took me a few searches, I also happened to get the brackets wrong for the array....used [ instead of (... and then went "heck, [ is for anonymous arrays..." hahaha. The map and grep took google searches to get it flowing back.

C:\>more first.pl #use warnings; #use strict; my @arr = (1..10); foreach my $num (@arr) { print "Index is: $num. Value is: $arr[$num] "; } #Another way print "\nWith a C like for loop\n"; for($num = 0; $num<= $#arr;$num++) { print "Index is: $num. Value is: $arr[$num] "; } my @divbytwo = map { $_ / 2} @arr; print "\n\@divbytwo = @divbytwo\n"; my @divbytwoo = grep { $_ % 2 == 0} @arr; print "\@divbytwoo = @divbytwoo\n"; C:\>perl first.pl Index is: 1. Value is: 2 Index is: 2. Value is: 3 Index is: 3. Value i +s: 4 Index is: 4. Value is: 5 Index is: 5. Value is: 6 Index is: 6. V +alue is: 7 Index is: 7. Value i s: 8 Index is: 8. Value is: 9 Index is: 9. Value is: 10 Index is: 10. +Value is: With a C like for loop Index is: 0. Value is: 1 Index is: 1. Value is: 2 Index is: 2. Value i +s: 3 Index is: 3. Value is: 4 Index is: 4. Value is: 5 Index is: 5. V +alue is: 6 Index is: 6. Value i s: 7 Index is: 7. Value is: 8 Index is: 8. Value is: 9 Index is: 9. Va +lue is: 10 @divbytwo = 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 @divbytwoo = 2 4 6 8 10

I think I better start from scratch :)

Replies are listed 'Best First'.
Re^2: Good Perl book?
by Athanasius (Archbishop) on Feb 11, 2017 at 02:45 UTC

    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,

      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://1181685]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found