Re: search & relplace question



John W. Krahn <krahnj@xxxxxxxxx> wrote in comp.lang.perl.misc:
> Tassilo v. Parseval wrote:
> > Also sprach Ashwin:
> >
> >>I am not able to understand why
> >>my @result =map ((s/BBI_DIR/BBI_DIR\\RBI/), @BBY); modifies the @BBY
> >>array,
> >>what I expected was that @result should get updated with replaced lines,
> >>but it only updated with value 1 , which is what search function
> >>returns on success
> >
> > Precisely.
> >
> >>if I try
> >>
> >>my @result =map ((s/BBI_DIR/BBI_DIR\\RBI/,$_), @BBY);
> >>@results and @BBY get the same value , but @result also gets those line
> >>which are replaces appended with 1,
> >>what I need is only @result to get updated
> >
> > You can make a copy of $_ in the map block and apply s/// to that. Or
> > you use List::MoreUtils::apply() (courtesy of Brian McCauley) which is
> > just like map() only that it wont alter the input elements:
> >
> > use List::MoreUtils qw/apply/;
> >
> > my @result = apply { s/BBI_DIR/BBI_DIR\\RBI/ } @BBY;
>
> Another way to do it:
>
> s/BBI_DIR/BBI_DIR\\RBI/ for my @result = @BBY;

Ah... very nice. That's the array version of the scalar idiom

( my $result = $RBY) =~ s/.../.../;

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.



Relevant Pages