Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] (unknown)

Expand Messages
  • Hans Ginzel
    ... Why write you $_ =~ s/.../.../? Command substitue operates on $_ if noone is specified via ~=, !=. See perldoc -f s; man perlop pragraph
    Message 1 of 5 , Nov 30, 2002
    • 0 Attachment
      On Fri, Nov 29, 2002 at 05:42:50PM -0600, Charles K. Clarkson wrote:
      > IG T [goes_t_idia@...] wrote:
      >
      > : I am trying to remove the '.txt' from the
      > : elements in @list any solution?
      > :
      > : my @list = qw( 1.txt 2.txt 3.txt 4.txt );
      > : my @other = map { $_ =~ s/\.txt//gi; } @list;

      Why write you $_ =~ s/.../.../?

      Command substitue operates on $_ if noone is specified via ~=, !=.
      See perldoc -f s; man perlop pragraph 's/PATTERN/REPLACEMENT/egimosx'.

      Why do you specify 'g' option to substitute?

      Why does you not anchor (to end of string) the expression: s/\.txt$/i?
      Or dou you realy substitute 'file.txt.gz' to 'file.gz'?
      man perlre, http://www.perldoc.com/perl5.8.0/pod/perlre.html.

      > : print @other;
      >
      > It does work. Just not the way you intend.
      > Each time 'map' sees an element of @list it
      > tries a substitution. It then returns the
      > number of times it substituted the expression.

      But it _realy does_ the subtition on the $_ variable, which is by
      'map' aliased to the @list items. Print the @list array.

      Solution using the map command would be:

      @other = map { ($a=$_) =~ s/\.txt$//i; $a } @list
      OR
      map { s/\.txt$//i; } (my @other = @list);

      > The most common idiom for doing this is
      > the foreach block or the statement modifier.

      s/\.txt$//i foreach (my @other = @list);

      I left onto you to benchmark wich of these is faster.
      man perlfaq3 'How do I profile my Perl programs?'.

      --
      http://www.parrotcode.org/
      http://practicalperl.com/slides/L02/slide_000.html
    Your message has been successfully submitted and would be delivered to recipients shortly.