Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

new to perl, need some help

by kingpin007 (Initiate)
on Oct 02, 2012 at 03:59 UTC ( #996795=perlquestion: print w/ replies, xml ) Need Help??
kingpin007 has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone! Im new to perl and was doing some programming and have a quick question about something that I cannot figure out. Within the foreach loop below, i am trying to save the values that meet the condition of the if/else statement within the array @values. Yet, rather than it printing out all the values, it only prints out 1, what am I doing wrong? I feel like I am not pushing the values into the array correctly
!#/usr/bin/perl @number = qw(1 2 3 4 5 10 11 12 13 14); print "The values within the array are @number\n\n"; print "We will now print the square root of each number in the array a +s long as it falls between the range of 100-200 \n\n"; foreach $num (@number) { $sq = $num * $num; if ($sq >= 100 && $sq <= 200) { @values = (); push(@values, $sq); } } print "Obtaining square values using foreach loop: @values\n";

Comment on new to perl, need some help
Download Code
Re: new to perl, need some help
by NetWallah (Abbot) on Oct 02, 2012 at 04:19 UTC
    Welcome!

    The first thing monks will tell new perl programmers is to always
    use strict;
    use warnings

    at the start of each program.

    That said, your problem is the use of

    @values = ();
    inside the loop. That re-initializes the @values array each time.
    Move that outside the loop, and initialize it like this:
    my @values = ();
    The Parens are not necessary - perl initializes it to an empty array anyway.
    Also, you code could be shortened if you used the "map" and "grep" functions, but I expect you have a ways to go before you are comfortable with them.
    But first - get your program working.
    Cheers, and all the best.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Ah okay, thank you so much for that! What a silly mistake I didnt even realize that. And yes, i had actually used grep/map in my code as well to get the same output
      @number = qw(1 2 3 4 5 10 11 12 13 14); @num = map ($_ *$_, (@number)), "\n"; @square = grep ($_ >=100 && $_ <=200, @num); print "Obtaining square values using map/grep: @square \n";
      I just wanted to learn how to do it through a foreach loop. Thanks again for the quick response and help! :)

      Here is issue with initialisation of array(@values = ();). It is reinitialising on every iteration of loop and ultimately storing last value. Use below hope that will fulfil your requirement

      !#/usr/bin/perl use strict; use warnings; @number = qw(1 2 3 4 5 10 11 12 13 14); print "The values within the array are @number\n\n"; print "We will now print the square root of each number in the array a +s long as it falls between the range of 100-200 \n\n"; my @values = (); foreach $num (@number) { $sq = $num * $num; if ($sq >= 100 && $sq <= 200) { push(@values, $sq); } } print "Obtaining square values using foreach loop: @values\n";
Re: new to perl, need some help
by marquezc329 (Scribe) on Oct 02, 2012 at 06:20 UTC
    Hi! I'm still pretty new to Perl/PerlMonks as well, but I'd really recommend taking a look at perldebtut in the documentation. It talks about why you should start with
    use strict; use warnings;
    as NetWallah recommended. Also, you will learn how to use the Perl debugger to step through one line of code at a time and really get a chance to watch as all the action happens. Like I said I'm still fairly new and learning more everyday, but stopping for frequent trips to the debugger to examine loops/conditionals, and especially as I learn to build more complex data stuctures, has not only helped me tremendously in writing working code and solving problems myself, but in UNDERSTANDING not only why certain pieces of code don't work, but often more importantly, why they do work. I'd take a look at the tutorial and then try to use the original code you posted to practice. I'll bet you can see exactly where @values reinitializes/empties on each round through the loop. Good Luck!
Re: new to perl, need some help
by kcott (Abbot) on Oct 02, 2012 at 06:48 UTC

    G'day kingpin007,

    Welcome to the monastery.

    I see NetWallah++ has provided the answer to your posted question. Here's a couple of additional points.

    Your shebang line needs to start with #!, not !#. I suspect this is simply a typo but if you run this code you'll get the (possibly rather cryptic) message: "Can't modify not in scalar assignment".

    When you have sequences of numbers, you can use the range operator (..). Instead of qw(1 2 3 4 5 10 11 12 13 14), you can just type (1 .. 5, 10 .. 14). This is described in perlop - Range Operators.

    -- Ken

Re: new to perl, need some help
by ww (Bishop) on Oct 03, 2012 at 03:28 UTC
    And yet another nit - except that it's really NOT a small thing.

    When you post a question, make sure you say what you mean. Your original post announces, in the code, that you will now print the square root of each number in the array as long as it falls between the range of 100-200 \n\n";.

    Of course, as you obviously know, none of the values in your array has a "square root" anywhere near the range 100..200. The next line of code makes your intent clear, but some will stop reading after the error, having analysed the post as "careless," at best. That could cost you valuable help.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-09-22 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (191 votes), past polls