Re: map function has me confused

2001-06-16 Thread Jos I. Boumans
ok, what's going on here is the following: map 'maps' (returns to whatever is on the left hand side of it) the return value of whatever is in between the { } after the 'map' statement; so if you say: my @foo = qw(bar baz); my @bar = map { s/a/e/ } @foo; print @bar; that will print

Re: map function has me confused

2001-06-15 Thread Me
> > [using map to transform an old array to a new array > > leaving the old one unchanged?] > > Here's my best shot: > > map { my $foo = $_; $foo =~ s/qux/waldo/ and $foo } @bar; Fwiw, I just thought of a more brief and elegant way: @newarray = map { local $_ = $_; s/foo/bar/; $_ } @o

Re: map function has me confused

2001-06-12 Thread Randal L. Schwartz
> "Chas" == Chas Owens <[EMAIL PROTECTED]> writes: Chas> On 12 Jun 2001 11:32:46 -0700, Randal L. Schwartz wrote: Chas> >> For this, I much prefer: >> >> s/foo/bar/ for @new = @original; >> >> And it's even less typing! Chas> Chas> Could you break this down into English? Copy @origin

Re: map function has me confused

2001-06-12 Thread Chas Owens
On 12 Jun 2001 11:32:46 -0700, Randal L. Schwartz wrote: > For this, I much prefer: > > s/foo/bar/ for @new = @original; > > And it's even less typing! Could you break this down into English? -- Today is Pungenday, the 17th day of Confusion in the YOLD 3167 P'tang!

Re: map function has me confused

2001-06-12 Thread Randal L. Schwartz
> "Michael" == Michael Fowler <[EMAIL PROTECTED]> writes: Michael> On Tue, Jun 12, 2001 at 05:56:17PM +0100, Tom Watson wrote: >> I was thinking it might be possible to do this in one line using map..? Michael> Well, there's: Michael> @new = map { my $tmp = $_; $tmp =~ s/foo/bar/; $

Re: map function has me confused

2001-06-12 Thread Michael Fowler
On Tue, Jun 12, 2001 at 05:56:17PM +0100, Tom Watson wrote: > I was thinking it might be possible to do this in one line using map..? Well, there's: @new = map { my $tmp = $_; $tmp =~ s/foo/bar/; $tmp; } @original; OR (this is bad form, you shouldn't use map in void context) map {

Re: map function has me confused

2001-06-12 Thread Tom Watson
>me said: > And you want it in one simple statement? I don't think > it can be done, depending on one's definition of simple. > > Here's my best shot: > > map { my $foo = $_; $foo =~ s/qux/waldo/ and $foo } @bar; Actually it was more of a question if it can't be done well, that's ok too

Re: map function has me confused

2001-06-12 Thread Me
> What I was looking for is a way to take an > element from @foo - change it in some way, > and place it in @bar without changing the > original element of @foo but do it in one > line of code: Well, I'd still be inclined to do something like: @foo = @bar; for (@foo) { s/qux/waldo/ }; e

Re: map function has me confused!

2001-06-12 Thread Me
Er, self-correction self-correction: > First: > /5/ =~ "line 5"; > > is done in list context. More accurately, something like: $_ = "line 5"; @onemap = (/5/); push (@allmaps, @onemap); where @onemap is really an unnamed internal list not available to your perl code and @allmap

RE: map function has me confused

2001-06-12 Thread Tom Watson
>me said: >> How would I go about creating a second array using map >> or grep without changing the original? Can I do that at all? >@foo = @bar; Sorry, I should have been clearer on this question. What I was looking for is a way to take an element from @foo - change it in some way, and pla

Re: map function has me confused!

2001-06-12 Thread Me
> > I don't fully understand how, when or why to use the map function - > > > > my @file = ("line 1", "line 2", "line 3", "line 4", "line 5"); > > my @test = map {/5/} @file; > > > > The result when I print @test is: > > 1 > > > > As I understand things, this is the m/5/ being "true" > > Sort

Re: map function has me confused!

2001-06-12 Thread Me
> I don't fully understand how, when or why to use the map function - > > my @file = ("line 1", "line 2", "line 3", "line 4", "line 5"); > my @test = map {/5/} @file; > > The result when I print @test is: > 1 > > As I understand things, this is the m/5/ being "true" Sorta. It's the length of