in reply to looping through array

It'd be very neat if you separated your code and placed it within the special "<code>" and "</code>" tags.

"Do we need to declare array elemnts in double quotes"

@array1 = ('one','two','three'); @array2 = ("four","five","six"); print "@array1\n@array2"; #OUTPUT one two three four five six
You can use Perl's quote like operators instead if you did not want to use quotes, however, using single quotes and double quotes can be critical when you want to interpolate ...consider the following quick example and see the differences in the output...
@array = qw(one two three); print @array, "\n"; print "@array\n"; print '@array\n'; __END__ onetwothree #array elements are fused. one two three #spaced and the "\n" replaced with a new line. @array\n #prints @array and "\n" as literals.
Let's expand on... Guess what the outcome of the code below is, as an exercise :
use strict; #a good habit to start your program with line use warnings; #another good habit too... my @array1 = ("one", "two", "five"); my @array2 = ("three","four"); my @array3 = ("one","two",@array2,"five"); my @array4 = ("one","two",'@array2',"five"); my @array5 = ("one","two","@array2","five"); print "@array3\n"; print "@array4\n"; print "@array5\n";

You can loop through your array with a for loop iteration too and using an incremented index allows you to access array elements in turns.

@array = qw(one two three four); for($index=0;$index<=$#array;$index++){ print $array[$index],"\n"; }
Read more at for loops and foreach loops, other than that, you can try and see for yourself if something really works and observe the behavior differences by experimenting on your options, this can let you discover new things as well as solidify your knowledge of something you learnt..

Update: Added examples...


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re^2: looping through array
by manishrathi (Beadle) on Jan 07, 2010 at 23:24 UTC
    @array = qw(one two three); print @array, "\n"; print "@array\n"; print '@array\n'; __END__ onetwothree #array elements are fused. one two three #spaced and the "\n" replaced with a new line. @array\n #prints @array and "\n" as literals.


    I understand first output as all the array elements are printed in a row.

    I dont understand second output. When print command is used with array name in quotes, output has a space between elements. where does this space come from ?

    In the third output, everything in single quotes is printed as literal. Does it mean that anything in single quotes is treated as literal and will be printed as is. Anything in double quotes will be treated as string value with variables getting its values from the code ?
      I dont understand second output. When print command is used with array name in quotes, output has a space between elements. where does this space come from ?

      The Perl print built-in prints a list with the $, special variable interpolated between each item.

      Double-quoted strings (which have nothing directly to do with print) interpolate arrays with the $" special variable interpolated between each array element. See the documentation in perlvar for the default values of the  $, and  $" special variables.

      In the third output, everything in single quotes is printed as literal. Does it mean that anything in single quotes is treated as literal and will be printed as is.

      Yes. Whether printed or not, there is no interpolation in single-quoted strings. The  \ (backslash) character has limited effect as an escape under conditions explained in the docs on single-quoted strings linked by other respondents.

      Anything in double quotes will be treated as string value with variables getting its values from the code ?

      Yes and no. Double-quoted strings interpolate scalar and array variables as per the docs.

      >perl -wMstrict -le "my @array = ('one', qw(two three), qq{four}); print @array; $, = 'FOO'; print @array; print qq{@array}; $\" = 'BAR'; print qq{@array}; " onetwothreefour oneFOOtwoFOOthreeFOOfour one two three four oneBARtwoBARthreeBARfour
      is anything within single quotes is always treated as literal in print statement ?
      anything in double quotes is treated as string with variables getting their values from the code ?
      whats the difference between when an array elements are declared in single quotes, double quotes and without quotes ? I tried out all the ways and I get the same result.
        is anything within single quotes is always treated as literal in print statement ? anything in double quotes is treated as string with variables getting their values from the code ?

        Asked and answered. See Re^3: looping through array.

        whats the difference between when an array elements are declared in single quotes, double quotes and without quotes ? I tried out all the ways and I get the same result.

        None. Strings can be defined using single-quotes, double-quotes and the qw operator (and probably in a few other ways that have slipped my mind). A list can be defined with strings and other things, and a list can be assigned to an array. Although not entirely unrelated, strings, lists and arrays are essentially independent.