In your first block of code you are modifiying the element of the array which is what you expect I believe. In your second block of code you are modifiying a new variable set at first equal to the value of the array element and then modified using the substition. The modification is not touching the array element. To make the second block have the same result you could change it to
 
$ar[$x] =~ s/Fred/Thomas/;
 
and then you are now, as in the first block, modifying the actual element of the array.


Andreas Karl Wittwer <[EMAIL PROTECTED]> wrote:
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


John V. Pataki
Logged in to my Yahoo Mail account on the web.
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to