strange behavior

M

Matija Papec

I thought that output should be the same for all four lines but they differ.
Am I running buggy perl? (5.8.0)


use strict;
use warnings;

for my $i (0..3) {
my $row=2;
my $tmp = join ' ', map { $_ += 5*$row } 0 .. 4;
print "$row - $tmp\n";
}

---------- Capture Output ----------
2 - 10 11 12 13 14
2 - 20 21 22 23 24
2 - 30 31 32 33 34
2 - 40 41 42 43 44
 
B

Bill

Matija said:
I thought that output should be the same for all four lines but they differ.
Am I running buggy perl? (5.8.0)


use strict;
use warnings;

for my $i (0..3) {
my $row=2;
my $tmp = join ' ', map { $_ += 5*$row } 0 .. 4;
print "$row - $tmp\n";
}

---------- Capture Output ----------
2 - 10 11 12 13 14
2 - 20 21 22 23 24
2 - 30 31 32 33 34
2 - 40 41 42 43 44

Yeah, the 0 .. 4 array is getting clobbered in the map function. (Don't
do that? :) )

Try:

============================
use strict;
use warnings;

print "\nMode 1\n";

for my $i (0..3) {
my $row=2;
my $tmp = join ' ', map { $_ += 5*$row } 0 .. 4;
print "$row - $tmp\n";
}


print "\nMode 2\n";

for my $j (0..3) {
$_ = 0;
my $row=2;
my $tmp = join ' ', map { 5*$row + $_ } 0 .. 4;
print "$row - $tmp\n";
}

print "\nMode 3\n";

for my $i (0..3) {
my @a = (0 .. 4);
my $row=2;
my $tmp = join ' ', map { $_ += 5*$row } @a;
print "$row - $tmp\n";
}


==============================
 
M

Matija Papec

X-Ftn-To: Bill

Yeah, the 0 .. 4 array is getting clobbered in the map function. (Don't
do that? :) )

Hm, but 0..4 isn't an array; it is a _list_ which gets cached and AFAIK it
shouldn't behave like that?
 
B

Bill

Matija said:
X-Ftn-To: Bill






Hm, but 0..4 isn't an array; it is a _list_ which gets cached and AFAIK it
shouldn't behave like that?
This is perl5-porters stuff. It's beyond me. I'd comment that IMO 0 .. 4
should be constant, and assigning to it should be a run time error. But
that's _my_ perl implementation, which exists only in my head (just as
well too :) ).
 
M

Michele Dondi

I thought that output should be the same for all four lines but they differ.
Am I running buggy perl? (5.8.0)
my $tmp = join ' ', map { $_ += 5*$row } 0 .. 4; ^^
^^

print "$row - $tmp\n";
}

---------- Capture Output ----------
2 - 10 11 12 13 14
2 - 20 21 22 23 24
2 - 30 31 32 33 34
2 - 40 41 42 43 44

Althoug that += is strange and unneeded, I can't understand why you
get that output and I'd say it's not correct. However I get it too!
(perl 5.8.3 here)

But then do you have a good reason for not using

map { $_ + 5*$row } 0..4;

instead?

Also, as a side note (you probably know that)

my @tmp = map { $_ + 5*$row } 0 .. 4;
print "$row - @tmp\n";


Michele
 
M

Matija Papec

X-Ftn-To: Michele Dondi

Michele Dondi said:
Althoug that += is strange and unneeded, I can't understand why you
get that output and I'd say it's not correct. However I get it too!
(perl 5.8.3 here)

I guess then, these who have older perls can stay at their rest since all
versions are acting like this. :)
But then do you have a good reason for not using

map { $_ + 5*$row } 0..4;

instead?

I wanted to keep code readable so,

my $tmp = join ' ', map { $_+=5*$row; "<!--FTD-$_-->" } 0 .. 4;
was more appealing then,

my $tmp = join ' ', map "<!--FTD-". $_+5*$row ."-->", 0 .. 4;
 
M

Matija Papec

X-Ftn-To: Bill

Bill said:
This is perl5-porters stuff. It's beyond me. I'd comment that IMO 0 .. 4
should be constant, and assigning to it should be a run time error. But
that's _my_ perl implementation, which exists only in my head (just as
well too :) ).

I think that explicit lists are rising run time error and ranges are not,
but I can't put my hand in fire on that. ;)
 
B

Ben Morrow

Quoth Matija Papec said:
X-Ftn-To: Michele Dondi



I guess then, these who have older perls can stay at their rest since all
versions are acting like this. :)


I wanted to keep code readable so,

my $tmp = join ' ', map { $_+=5*$row; "<!--FTD-$_-->" } 0 .. 4;
was more appealing then,

my $tmp = join ' ', map "<!--FTD-". $_+5*$row ."-->", 0 .. 4;

But what's wrong with

.... map { my $x = $_ + 5*$row; "<!--FTD-$x-->" } ...

?

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top