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

hi guys , I am new to using push
I did the following

if ( $score > 12 ) && ( $score < 100 ) { push @thescore, $_};

the problem I am having is how to open that array : I tried foreacsh and print @thescore but I only see the last score

somthing like

for(@thescore) { print "$thescore\n"; }

thanks for help

Replies are listed 'Best First'.
Re: using push
by Tanalis (Curate) on Jan 24, 2003 at 15:25 UTC
    Your problem's not with push, it's in your for loop.

    When you use something like foreach (@thescore) each element of @thescore is placed into the "default" variable, $_, unless you tell it different.

    So, something like

    foreach (@thescore) { print $_, "\n"; }
    would work for what you have above.

    If you specifically wanted to assign the data to, for example, the $score variable, you can do this by specifying the variable when you define the loop:

    foreach my $score (@thescore) { print $score, "\n"; }
    Hope that helps a little.
    -- Foxcub
Re: using push
by dragonchild (Archbishop) on Jan 24, 2003 at 15:28 UTC
    1. USE strict!!!
    2. USE my!!!
    3. Learn how foreach works.
    Try something like:
    use strict; my @scores. while (my $score = <>) { last if $score =~ /\D/; push @scores, $score; } foreach my $score (@scores) { print "The score was $score\n"; }
    That will take a bunch of scores from the command-line (until you hit Ctrl-D or type a non-numeric character), then print them all out for you in the order you entered them.

    Note - because I scoped my variables and am using strict, if you try and print $score between the while loop and the foreach loop, you will end up with an error.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: using push
by dempa (Friar) on Jan 24, 2003 at 15:52 UTC

    Your question is already answered. I would just like to give some friendly advice. Take a look at that 'if'. It's not going to work. Two problems:

    The if produces a syntax error. Use this instead: if ($score > 12 and $score < 100)

    $_ isn't assigned to anything. Replace it with: push @thescore,$thescore.

    update: Ehrm, ofcourse, the second code should be push @thescore,$score. Thanks to various people msg:ing me and pointing that out. Apparently I wasn't 100% awake when I posted this. :)

    -- 
    dempa

      thanks all
Re: using push
by Steve_p (Priest) on Jan 24, 2003 at 17:51 UTC

    You have to make sure that @thescore is declared outside of the loop. Otherwise, Perl, and nearly any other programming language, will see @thescore as having a local scope inside the loop only. Also, repeat the typical PM manta of using warnings and strict to run your code.

    #!/usr/bin/perl -w use strict; ... my @thescore = (); #declare and initialize the array if ( $score > 12 ) && ( $score < 100 ){ push @thescore, $_}; ... }

    Also, in your code in the loop is looking for a variable called $thescore. The value you are trying to print will be in $_ instead. Here you can do two different things.

    foreach (@thescore) { print "$_\n"; }

    or

    foreach my $thescore(@thescore) { print "$thescore\n"; }