Results 1 to 7 of 7

Thread: Basic Perl text substitution

  1. #1
    Join Date
    Jan 2009
    Location
    Rochester NY
    Beans
    92
    Distro
    Ubuntu 13.10 Saucy Salamander

    Question Basic Perl text substitution

    Hello,

    I would like to combine these two Perl substitutions

    Code:
    my $name = $file;
    $name =~ s/.out$//;
    $name =~ s/^nmr//;
    into a single command, just like sed can do:

    Code:
    sed -e 's/^nmr//g' -e 's/out$//g'
    I know that Perl was meant to mimic the way that sed works, but it doesn't work *exactly* the same.

    The program I have now works, but I would like to make it better.

    Thanks very much!

  2. #2

    Re: Basic Perl text substitution

    Like this?

    Code:
    perl -pe 's/.out$//; s/^nmr//'

  3. #3
    Join Date
    Nov 2009
    Location
    Australia
    Beans
    57
    Distro
    Ubuntu

    Re: Basic Perl text substitution

    How about ..
    Code:
    $name =~  s/.out$|^nmr//g;

  4. #4
    Join Date
    Jan 2009
    Location
    Rochester NY
    Beans
    92
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Basic Perl text substitution

    Hi wallaroo,

    Yep that worked!

    Thanks so much!

    and to trent.josephsen, I should have specified that I meant with a perl text file. My bad!

    -Dave

  5. #5
    Join Date
    Mar 2006
    Beans
    393

    Re: Basic Perl text substitution

    Quote Originally Posted by hailholyghost View Post
    Hello,

    I would like to combine these two Perl substitutions

    Code:
    my $name = $file;
    $name =~ s/.out$//;
    $name =~ s/^nmr//;
    into a single command, just like sed can do:

    Code:
    sed -e 's/^nmr//g' -e 's/out$//g'
    I know that Perl was meant to mimic the way that sed works, but it doesn't work *exactly* the same.

    The program I have now works, but I would like to make it better.

    Thanks very much!
    Why? Doing so makes your code harder to understand. Avoid micro-optimization; it never improves the code.
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication as it is about coding.

  6. #6

    Re: Basic Perl text substitution

    shawnhcorey is correct; combining the two into a single substitution is not an improvement (and will probably hurt your performance rather than the reverse).

    Also, I'd like to observe that . matches any character, so your first substitution will change 'about' to 'a'. If you want to match a literal ., escape it.

  7. #7
    Join Date
    Aug 2007
    Location
    Sweden
    Beans
    197
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Basic Perl text substitution

    Quote Originally Posted by hailholyghost View Post
    I would like to combine these two Perl substitutions

    Code:
    my $name = $file;
    $name =~ s/.out$//;
    $name =~ s/^nmr//;
    into a single command
    All the answers above are correct and works. My answer is a different approach.

    If we look at it as idioms, the above is basically "remove things we don't want" so at the end "what remains is what we want". I'll suggest "pick out what we want" and in this case "express what we want by its context".

    Code:
    if ($file =~ m/^nmr(.*)\.out$/) { 
      $name = $1;
    }
    The thing in parenthesis is captured in variable $1 (and $2 etc).

    This pick things behaves slightly different in certain cases, compared to the remove things. If $file does not match, $name will be unset, instead of being a copy of $file. If that is good or bad depends on the rest of the program. For example, if it would be an error for $file not to have "nmr" and ".out" in it the code could be extended like this:

    Code:
    if ($file =~ m/^nmr(.*)\.out$/) {
      $name = $1;
    } else {
      die "file name didn't contain a name\n";
    }
    Another example (where both context and content matters): if $name must be a number, then change the pattern to ^nmr(\d+)\.out$ .

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •