f



strange behavior

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



-- 
Matija
0
Matija
5/29/2004 6:38:12 PM
comp.lang.perl.misc 33233 articles. 1 followers. brian (1246) is leader. Post Follow

8 Replies
478 Views

Similar Articles

[PageSpeed] 25

Matija Papec wrote:
> 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";
   }


==============================


0
Bill
5/29/2004 8:56:43 PM
X-Ftn-To: Bill 

Bill <wherrera@lynxview.com> wrote:
>>     my $tmp = join ' ',  map { $_ += 5*$row } 0 .. 4;
<snip>

>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?



-- 
Matija
0
Matija
5/30/2004 9:00:39 AM
Matija Papec wrote:
> X-Ftn-To: Bill 
> 
> Bill <wherrera@lynxview.com> wrote:
> 
>>>    my $tmp = join ' ',  map { $_ += 5*$row } 0 .. 4;
> 
> <snip>
> 
>>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?
> 
> 
> 
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 :) ).

0
Bill
5/30/2004 2:15:40 PM
On Sat, 29 May 2004 20:38:12 +0200, Matija Papec <perl@my-header.org>
wrote:

>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
-- 
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
  "perl bug File::Basename and Perl's nature"
0
Michele
5/30/2004 6:35:13 PM
X-Ftn-To: Michele Dondi 

Michele Dondi <bik.mido@tiscalinet.it> wrote:
>>    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)

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;



-- 
Secure and instant E-pay for web shops
http://www.ouroboros.hr/?e-placanje
0
Matija
5/31/2004 8:15:34 PM
X-Ftn-To: Bill 

Bill <wherrera@lynxview.com> wrote:
>> 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 :) ).

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. ;)


-- 
Secure and instant E-pay for web shops
http://www.ouroboros.hr/?e-placanje
0
Matija
5/31/2004 8:20:40 PM
Quoth Matija Papec <perl@my-header.org>:
> X-Ftn-To: Michele Dondi 
> 
> Michele Dondi <bik.mido@tiscalinet.it> wrote:
> >>    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)
> 
> 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;

But what's wrong with

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

?

Ben

-- 
                Outside of a dog, a book is a man's best friend.
                Inside of a dog, it's too dark to read.
ben@morrow.me.uk                                                  Groucho Marx
0
Ben
5/31/2004 8:47:00 PM
On Mon, 31 May 2004 20:47:00 +0000 (UTC), Ben Morrow
<usenet@morrow.me.uk> wrote:

>> 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-->" } ...
>
>?

Nothing, if you ask me.


0
Matija
6/1/2004 12:31:13 PM
Reply: