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:
Syntax question.

 



Omore2ac
Novice

Apr 28, 2011, 9:03 AM

Post #1 of 6 (925 views)
Syntax question. Can't Post

Hey all,



I just came across this part of script and it makes no sense to me:

(my $idx = 0; $idx <= $#$row; $idx++)

$idx is declared and assigned; ????????????; autoincrement through $idx.

I know the big arrow is pretty much a comma to Perl and $#row is the last element in @any_array...however $#$row?!?!?

Can anyone enlighten this confused Perler?


BillKSmith
Veteran

Apr 28, 2011, 10:24 AM

Post #2 of 6 (922 views)
Re: [Omore2ac] Syntax question. [In reply to] Can't Post

This is probably part of the start of a C-style for loop. Refer: perldoc perlsyn


Code
  

for (my $idx = 0; $idx <= $#$row; $idx++) {

***

}

Good Luck,
Bill


Omore2ac
Novice

Apr 28, 2011, 11:01 AM

Post #3 of 6 (921 views)
Re: [BillKSmith] Syntax question. [In reply to] Can't Post

Thank you Bill, Much appreciated. I'll check the perldocs.


miller
User

Apr 28, 2011, 8:14 PM

Post #4 of 6 (907 views)
Re: [Omore2ac] Syntax question. [In reply to] Can't Post

Yes, $#array is the last index of @array.


Code
my @array = ('a','b','c'); 
print $#array; # Outputs 2


However, it's possible to assign an array reference to a scalar, and that is how you would access the last index of that array.


Code
my $arrayref = ['a','b','c','d','e']; 
print $#$arrayref; #outputs 4


I'd like to note that your for loop is just an incrementer and can be simplied from


Code
for (my $idx = 0; $idx <= $#$row; $idx++) {


To


Code
for my $idx (0..$#$row) {


- Miller


shawnhcorey
Enthusiast


Apr 29, 2011, 6:43 AM

Post #5 of 6 (862 views)
Re: [Omore2ac] Syntax question. [In reply to] Can't Post

The preferred technique is to use a foreach loop:

Code
for my $idx ( 0 .. $#$row ){ 
...
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


rovf
Veteran

Apr 29, 2011, 7:55 AM

Post #6 of 6 (856 views)
Re: [Omore2ac] Syntax question. [In reply to] Can't Post

What you call "big arrow" is a comparision operator (numerically less-or-equal).

Otherwise, it doesn't make sense if you post just an arbitrary snippet from a program, without the larger context.

 
 


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

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