>From Programming Perl, 3d ed, Section 4.4.3:
If LIST consists entirely of assignable values (meaning variables,
generally, not enumerated constants), you can modify each of those
variables by modifying VAR inside the loop. That's because the foreach
loop index variable is an implicit alias for each item in the list that
you're looping over. Not only can you modify a single array in place,
you can also modify multiple arrays and hashes in a single list:
foreach $pay (@salaries) { # grant 8% raises
$pay *= 1.08;
}
for (@christmas, @easter) { # change menu
s/ham/turkey/;
}
s/ham/turkey/ for @christmas, @easter; # same thing
for ($scalar, @array, values %hash) {
s/^\s+//; # strip leading whitespace
s/\s+$//; # strip trailing whitespace
}
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Andreas Karl Wittwer
Sent: Saturday, May 08, 2004 2:33 PM
To: ActiveState
Subject: Array behavior: please expalin ...
Hi,
I thought I know a bit of perl but this is confusing ...
=== cut 1 ===
#! /usr/bin/perl
use strict;
my @ar = ();
push @ar,"Harry Fred Paul";
push @ar,"Fred Ringo John";
push @ar,"Georg Don Fred";
foreach my $l (@ar) {
$l =~ s/Fred/Thomas/;
}
t();
sub t {
for(my $z=0;$z<@ar;$z++) {
my $l = $ar[$z];
if($l =~ /Fred/) {
print "Fred joined\n";
} else {
print "fred missed\n" ;
}
}
}
fred missed
fred missed
fred missed
=== end cut 1 ===
=== cut 2 ===
#! /usr/bin/perl
use strict;
my @ar = ();
push @ar,"Harry Fred Paul";
push @ar,"Fred Ringo John";
push @ar,"Georg Don Fred";
for( my $x=0;$x<@ar;$x++) {
my $l = $ar[$x];
$l =~ s/Fred/Thomas/;
}
t();
sub t {
for(my $z=0;$z<@ar;$z++) {
my $l = $ar[$z];
if($l =~ /Fred/) {
print "Fred joined\n";
} else {
print "fred missed\n" ;
}
}
}
Fred joined
Fred joined
Fred joined
=== end cut 2 ===
Someone able to explain. please?
Best regards,
Andreas Karl Wittwer
Phone: +49-7052-92206
FAX: +49-7052-92208
Mobil: +49-172-542 541 4
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs