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";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|