You can loop through a list (we're supposed to call them "lists" not "arrays" in Perl these days!) using a foreach loop running a counter, or using the second form of foreach that doesn't provide a counter. Thus
[source link]:
#!/usr/bin/perl
@demo1 = (20,30,50,60,80,200);
@demo2 = (40,70,90,40,20,10,0);
for ($k=0; $k<@demo1; $k++) {
$demo1[$k] += 2;
}
print ("@demo1\n");
foreach $item (@demo2) {
$item += 3;
}
print ("@demo2\n");
and that runs as follows:
$ perl prog.pl
22 32 52 62 82 202
43 73 93 43 23 13 3
$

In the first form, you're providing a counter and know the element that you're on, thus you could make a change based on the element number. In the second form, you don't have a counter;1 however, many folks don't realise that any changes you make to the "loop variable" are saved back in the list.
In other words, the second form is a very neat way of altering every element of a list. There is no need to actually know the element number, which will allow you to simplify code in many (but not position number-dependent) places.
Note that this trick only works if you specify just an array name in the foreach statement. If you wrote
foreach $item(@this,@that)
then changes you make to $item are not reflected back in the @this and @that lists.
Further note: the words "for" and "foreach" are interchangeable, so you could save four more bytes if you really want.
Another possibility is to use the map function, or keep your own counter and use the second form of "for". So
[source link]:
!#/usr/bin/perl
@demo1 = (20,30,50,60,80,200);
@demo2 = (40,70,90,40,20,10,0);
@demo1 = map($_+7,@demo1);
print ("@demo1\n");
foreach $item (@demo2) {
$demo2[$n] += 9;
$n++;
}
print ("@demo2\n");
The map function is worthy of further study. Each element of a list is put into a special variable called $_ in turn. You can then perform any operation that you wish on that item.
$ prog2.pl
27 37 57 67 87 207
49 79 99 49 29 19 9
$
Illustration - delegates on a recent Perl course (written 2010-06-15, updated 2010-06-18)
23a7
Associated topics are indexed under
P208 - Perl - Lists [3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
[2295] The dog is not in trouble - (2009-07-17)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[1918] Perl Socket Programming Examples - (2008-12-02)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[968] Perl - a list or a hash? - (2006-12-06)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[773] Breaking bread - (2006-06-22)
[762] Huge data files - what happened earlier? - (2006-06-15)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[560] The fencepost problem - (2006-01-10)
[463] Splitting the difference - (2005-10-13)
[355] Context in Perl - (2005-06-22)
[240] Conventional restraints removed - (2005-03-09)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[28] Perl for breakfast - (2004-08-25)
Some other Articles
Setting a safety net or fallback value in PerlIntelligent Matching in Perlswitch and case, or given and when in PerlPython - splitting and joining stringsIterating over a Perl list and changing all itemsWhat is Perl?Igloos melt in the summer, but houses do notA course review - for the tutor to completeFrankfurt in 90 minutesFrom home to Nurnberg - journey pictures