Professional Web Applications Themes

Regex - assign to new variable and replace in one line? - PERL Miscellaneous

This is probably simple for the experienced Perl programmer, but as I am learning, and hours of searching has led to nothing, this one has tied me up long enough... So I could use a little assistance. I am trying to consolidate the following 2 lines, into 1 line. $missionname = $check[1]; $missionname =~ s/.mis//; I am trying to find a way to assign $missionname the same value as $check[1], and remove '.mis' from the variable, in one expression. This is really an efficiency issue, and also a learning experience for me. Thanks for any help, Brad...

  1. #1

    Default Regex - assign to new variable and replace in one line?

    This is probably simple for the experienced Perl programmer, but as I
    am learning, and hours of searching has led to nothing, this one has
    tied me up long enough... So I could use a little assistance.

    I am trying to consolidate the following 2 lines, into 1 line.

    $missionname = $check[1];
    $missionname =~ s/.mis//;

    I am trying to find a way to assign $missionname the same value as
    $check[1], and remove '.mis' from the variable, in one expression.
    This is really an efficiency issue, and also a learning experience for
    me.

    Thanks for any help,
    Brad
    Brad Walton Guest

  2. #2

    Default Re: Regex - assign to new variable and replace in one line?

    Brad Walton wrote:
    > This is probably simple for the experienced Perl programmer, but as
    > I am learning, and hours of searching has led to nothing, this one
    > has tied me up long enough... So I could use a little assistance.
    >
    > I am trying to consolidate the following 2 lines, into 1 line.
    >
    > $missionname = $check[1];
    > $missionname =~ s/.mis//;
    >
    > I am trying to find a way to assign $missionname the same value as
    > $check[1], and remove '.mis' from the variable, in one expression.
    ($missionname = $check[1]) =~ s/\.mis//;

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  3. #3

    Default Re: Regex - assign to new variable and replace in one line?

    X-Ftn-To: Brad Walton

    [email]jw1454sbc.com[/email] (Brad Walton) wrote:
    >This is probably simple for the experienced Perl programmer, but as I
    >am learning, and hours of searching has led to nothing, this one has
    >tied me up long enough... So I could use a little assistance.
    >
    >I am trying to consolidate the following 2 lines, into 1 line.
    >
    >$missionname = $check[1];
    >$missionname =~ s/.mis//;
    s/.mis// for $missionname = $check[1];

    this one is most common and recommended:
    ($missionname = $check[1]) =~ s/.mis//;



    --
    Matija
    Matija Papec Guest

  4. #4

    Default Re: Regex - assign to new variable and replace in one line?

    Brad Walton wrote:

    ....
    > $missionname = $check[1];
    > $missionname =~ s/.mis//;
    >
    > I am trying to find a way to assign $missionname the same value as
    > $check[1], and remove '.mis' from the variable, in one expression.
    > This is really an efficiency issue, and also a learning experience for
    > me.
    ....

    > Brad
    >
    You indicate you are trying to remove the string '.mis' from the
    variable. To do that, you should escape the . as it is a metacharacter.
    If that is what you intend, something like:

    ($missionname = $check[1]) =~ s/\.mis//;

    should work.
    --
    Bob Walton

    Bob Walton Guest

  5. #5

    Default Re: Regex - assign to new variable and replace in one line?

    Matija Papec <mpapec> wrote:
    > [email]jw1454sbc.com[/email] (Brad Walton) wrote:
    >>I am trying to consolidate the following 2 lines, into 1 line.
    >>
    >>$missionname = $check[1];
    >>$missionname =~ s/.mis//;
    >
    > s/.mis// for $missionname = $check[1];
    I hadn't seen that before -- and now that I have, I think I'll try to forget
    it. :-)
    > this one is most common and recommended:
    > ($missionname = $check[1]) =~ s/.mis//;
    David K. Wall Guest

  6. #6

    Default Re: Regex - assign to new variable and replace in one line?

    Brad Walton wrote:

    (snipped)
    > I am trying to consolidate the following 2 lines, into 1 line.
    > $missionname = $check[1];
    > $missionname =~ s/.mis//;

    Below. This method of mine takes advantage of a
    recent topic, precedence order.


    Purl Gurl
    --

    #!perl

    Check = ("zero", "Purl Gurl.mis Rocks!");

    substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4, "");

    print $missionname;


    PRINTED RESULTS:
    ________________

    Purl Gurl Rocks!
    Purl Gurl Guest

  7. #7

    Default Re: Regex - assign to new variable and replace in one line?

    Purl Gurl wrote:
    > Brad Walton wrote:
    (snipped)
    > substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4, "");

    This code example of mine exemplifies how, at times,
    "thinking outside the box" can lead to better efficiency.

    For this case example, efficiency is improved an average
    twenty-nine percent, consistently.

    Purl Gurl
    --

    #!perl

    print "Content-type: text/plain\n\n";

    use Benchmark;

    print "Run One:\n\n";
    &Time;

    print "\n\nRun Two:\n\n";
    &Time;

    print "\n\nRun Three:\n\n";
    &Time;

    sub Time
    {
    timethese (100000,
    {
    'Cargo_Cult' =>
    'Check = ("zero", "Purl Gurl.mis Rocks!");
    ($missionname = $Check[1]) =~ s/.mis//;',

    'Purl_Gurl' =>
    'Check = ("zero", "Purl Gurl.mis Rocks!");
    substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4, "");',
    } );
    }


    PRINTED RESULTS:
    ________________

    Run One:

    Benchmark: timing 100000 iterations of Cargo_Cult, Purl_Gurl...
    Cargo_Cult: 1 wallclock secs ( 0.93 usr + 0.00 sys = 0.93 CPU) 107526.88/s
    Purl_Gurl: 1 wallclock secs ( 0.71 usr + 0.00 sys = 0.71 CPU) 140845.07/s


    Run Two:

    Benchmark: timing 100000 iterations of Cargo_Cult, Purl_Gurl...
    Cargo_Cult: 1 wallclock secs ( 0.99 usr + 0.00 sys = 0.99 CPU) 101010.10/s
    Purl_Gurl: -1 wallclock secs ( 0.71 usr + 0.00 sys = 0.71 CPU) 140845.07/s


    Run Three:

    Benchmark: timing 100000 iterations of Cargo_Cult, Purl_Gurl...
    Cargo_Cult: 1 wallclock secs ( 0.99 usr + 0.00 sys = 0.99 CPU) 101010.10/s
    Purl_Gurl: 1 wallclock secs ( 0.72 usr + 0.00 sys = 0.72 CPU) 138888.89/s
    Purl Gurl Guest

  8. #8

    Default Re: Regex - assign to new variable and replace in one line?

    > ($missionname = $check[1]) =~ s/\.mis//;

    Thank you everyone for your help. Works great!

    Brad
    Brad Walton Guest

  9. #9

    Default Re: Regex - assign to new variable and replace in one line?

    X-Ftn-To: Purl Gurl

    Purl Gurl <purlgurlpurlgurl.net> wrote:
    >> $missionname = $check[1];
    >> $missionname =~ s/.mis//;
    >
    >
    >Below. This method of mine takes advantage of a
    >recent topic, precedence order.
    >
    >Purl Gurl
    >--
    >
    >#!perl
    >
    >Check = ("zero", "Purl Gurl.mis Rocks!");
    >
    >substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4, "");
    Beside strange expression, this code doesn't do what OP wanted.



    --
    Matija
    Matija Papec Guest

  10. #10

    Default Re: Regex - assign to new variable and replace in one line?

    > > >Below. This method of mine takes advantage of a
    > > >recent topic, precedence order.
    >
    > > >Check = ("zero", "Purl Gurl.mis Rocks!");
    >
    > > >substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4,
    "");
    >
    > > Beside strange expression, this code doesn't do what OP wanted.
    >
    >
    > Your statement is untrue.

    I'll give it a test run and let you both know...

    Thanks,
    Brad


    Brad Walton Guest

  11. #11

    Default Re: Regex - assign to new variable and replace in one line?

    On Wed, 20 Aug 2003 15:56:38 -0700, Purl Gurl <purlgurlpurlgurl.net>
    wrote:
    >(topic is to make a "one liner" for following)
    >
    >> >> $missionname = $check[1];
    >> >> $missionname =~ s/.mis//;
    >
    >
    >> >Below. This method of mine takes advantage of a
    >> >recent topic, precedence order.
    >
    >> >Check = ("zero", "Purl Gurl.mis Rocks!");
    >
    >> >substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4, "");
    >
    >> Beside strange expression, this code doesn't do what OP wanted.
    >
    >Your statement is untrue.
    /.mis/ is regex and Brad didn't wrote s/\.mis//

    --
    Matija
    Matija Papec Guest

  12. #12

    Default Re: Regex - assign to new variable and replace in one line?

    "Brad Walton" <sammiegreatergreen.com> wrote in message news:<zzZ0b.213382$YN5.147714sccrnsc01>...
    > > > >Below. This method of mine takes advantage of a
    > > > >recent topic, precedence order.
    >
    > > > >Check = ("zero", "Purl Gurl.mis Rocks!");
    >
    > > > >substr (($missionname = $Check[1]), index ($missionname, ".mis"), 4,
    > "");
    >
    > > > Beside strange expression, this code doesn't do what OP wanted.
    > >
    > >
    > > Your statement is untrue.
    >
    >
    > I'll give it a test run and let you both know...
    This method seems to work fine also. I don't fully understand (most of
    it) however, so I will go with:

    ($missionname = $check[1]) =~ s/\.mis//;

    until I can better understand Gurl's method.

    Thanks again,
    Brad
    Brad Walton Guest

  13. #13

    Default Re: Regex - assign to new variable and replace in one line?

    (my $missionname = $check[1]) =~ s/.mis//;

    or

    s/.mis// for (my $missionname = $check[1])

    Quote Originally Posted by Brad Walton View Post
    This is probably simple for the experienced Perl programmer, but as I
    am learning, and hours of searching has led to nothing, this one has
    tied me up long enough... So I could use a little assistance.

    I am trying to consolidate the following 2 lines, into 1 line.

    $missionname = $check[1];
    $missionname =~ s/.mis//;

    I am trying to find a way to assign $missionname the same value as
    $check[1], and remove '.mis' from the variable, in one expression.
    This is really an efficiency issue, and also a learning experience for
    me.

    Thanks for any help,
    Brad
    Unregistered Guest

Similar Threads

  1. regex replace
    By AVL in forum ASP.NET Building Controls
    Replies: 1
    Last Post: March 30th, 11:03 AM
  2. RegEx replace?
    By joeDangelo in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: July 26th, 02:48 AM
  3. regex replace string garbling
    By David Stiller in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 3
    Last Post: May 12th, 01:57 PM
  4. Replies: 4
    Last Post: October 21st, 04:56 PM
  5. search and replace using regex
    By Tom Kinzer in forum PERL Beginners
    Replies: 5
    Last Post: December 6th, 03:40 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139