CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
variable declaration and countdown

 



jazzo
Novice

Aug 25, 2012, 10:41 AM

Post #1 of 4 (757 views)
variable declaration and countdown Can't Post

Hi peeps, I came across this simple script:

Code
 
#!/usr/bin/perl
# countdown.plx
use warnings;
use strict;
my @count = (1..5);
for (reverse(@count)) {
print "$_...\n";
sleep 1;
}
print "BLAST OFF!\n";

and I was wondering, shouldn't the variable

Code
reverse

be declare as

Code
my reverse

rather than using it without declaring it?
The the other thing. to print off the numbers we say:

Code
print "$_...\n";

why do we use "$_"?
can we not use the array something like

Code
print "$count[reverse]\n";

thanks


(This post was edited by jazzo on Aug 25, 2012, 10:48 AM)


FishMonger
Veteran / Moderator

Aug 25, 2012, 11:03 AM

Post #2 of 4 (754 views)
Re: [jazzo] variable declaration and countdown [In reply to] Can't Post

reverse is not a variable. It's one of perl's built-in functions.

http://perldoc.perl.org/functions/reverse.html


Laurent_R
Veteran / Moderator

Aug 25, 2012, 11:05 AM

Post #3 of 4 (753 views)
Re: [jazzo] variable declaration and countdown [In reply to] Can't Post

Hi,

reverse is not a variable, but a function that reverse the order of the @count array (makes it 5 4 3 2 1).

In the for loop, the default variable $_ is assigned successively to the reversed values of @count.

You could write this more explicitly:


Code
foreach my $number (reverse @count) { 
print "$number...\n";
sleep 1;
}
print "BOOOOM\n";



jazzo
Novice

Aug 25, 2012, 11:23 AM

Post #4 of 4 (749 views)
Re: [Laurent_R] variable declaration and countdown [In reply to] Can't Post

of course it is a function, what a donkey I am, sorry!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives