if I want to loop through this array using foreach, do I need to use  $data_array variabke only or can I use any variable ?
foreach $loopvariable (@data_array) { print $loopvariabe ; }
does it mean that  $loopvariable will be assigned the elelments of @data_array one by one ?
foreach (@data_array) { print $data_array ; } #this looks like wrong syntax
Do we need to declare array elemnts in double quotes only or can we declare it in single quotes also ? what if array elements are declared without any quotes ? will it be a syntax error ?

Not to discourage asking questions, but even without Resort to The Fine Manuals linked by other respondents, these questions can also be answered by experimentation. Note: Using warnings and strictures (use warnings; use strict; or  -wMstrict on the command line) while experimenting can be very informative.

>perl -wMstrict -le "my @array = qw(one two three); for my $loopvariable (@array) { print $loopvariable; } " one two three >perl -wMstrict -le "my @array = qw(one two three); for (@array) { print $array; } " Global symbol "$array" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

Note also that the loop variable is aliased to the elements of the list over which the loop iterates; it can be used to change them (if they are changeable!).

>perl -wMstrict -le "my @array = ('two', 'three', 'four'); for my $loopvariable (@array) { ++$loopvariable; } print qq{@array}; " twp thref fous

In reply to Re: looping through array by AnomalousMonk
in thread looping through array by manishrathi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.