in reply to looping through array
"Do we need to declare array elemnts in double quotes"
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...@array1 = ('one','two','three'); @array2 = ("four","five","six"); print "@array1\n@array2"; #OUTPUT one two three four five six
Let's expand on... Guess what the outcome of the code below is, as an exercise :@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.
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.
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..@array = qw(one two three four); for($index=0;$index<=$#array;$index++){ print $array[$index],"\n"; }
Update: Added examples...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: looping through array
by manishrathi (Beadle) on Jan 07, 2010 at 23:24 UTC | |
by AnomalousMonk (Archbishop) on Jan 08, 2010 at 02:50 UTC | |
by manishrathi (Beadle) on Jan 08, 2010 at 00:16 UTC | |
by AnomalousMonk (Archbishop) on Jan 08, 2010 at 03:07 UTC |