Print modified version of a string w/o intermediate variable or subroutine

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Oct 7, 2005.

  1. Guest

    I have a variable which contains a text string. I want to print a
    modified verion of the string without changing the variable. Consider
    this code, which simply reverses each occurance of the word "gas":

    my $word = 'gas';
    my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
    (my $temp = $string) =~ s/$word/reverse $word/ieg;
    print "$temp\n";

    That works, but it uses an intermediate variable ($temp). I dislike
    intermediate variables.

    I could create a subroutine, but that's even uglier.

    Is there an elegant way to print the modified version of the string
    without an intermediate variable or a subroutine?
    , Oct 7, 2005
    #1
    1. Advertising

  2. Re: Print modified version of a string w/o intermediate variableor subroutine

    wrote:
    > I have a variable which contains a text string. I want to print a
    > modified verion of the string without changing the variable. Consider
    > this code, which simply reverses each occurance of the word "gas":
    >
    > my $word = 'gas';
    > my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
    > (my $temp = $string) =~ s/$word/reverse $word/ieg;
    > print "$temp\n";
    >
    > That works, but it uses an intermediate variable ($temp). I dislike
    > intermediate variables.
    >
    > I could create a subroutine, but that's even uglier.
    >
    > Is there an elegant way to print the modified version of the string
    > without an intermediate variable or a subroutine?


    Depending on your definition of elegant:

    $ perl -le'
    my $word = q/gas/;
    my $string = "Jumping Jack Flash, It\047s a gas, gas, gas.";
    print $string;
    print map lc eq lc $word ? scalar reverse : $_, split /($word)/i, $string;
    '
    Jumping Jack Flash, It's a gas, gas, gas.
    Jumping Jack Flash, It's a sag, sag, sag.



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Oct 8, 2005
    #2
    1. Advertising

  3. Guest

    >John W. Krahn wrote:
    >>> print map lc eq lc $word ? scalar reverse : $_, split /($word)/i, $string;


    Clever. I didn't realize you could do something like "split /($word)/".
    That's handy to know.

    Thanks!
    , Oct 8, 2005
    #3
  4. Dr.Ruud Guest

    :

    > I have a variable which contains a text string. I want to print a
    > modified verion of the string without changing the variable. Consider
    > this code, which simply reverses each occurance of the word "gas":
    >
    > my $word = 'gas';
    > my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
    > (my $temp = $string) =~ s/$word/reverse $word/ieg;
    > print "$temp\n";
    >
    > That works, but it uses an intermediate variable ($temp). I dislike
    > intermediate variables.


    How do you feel about using '$_'?

    $_ = $string;
    s/$word/reverse $word/ieg;
    print $_, "\n";

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Oct 8, 2005
    #4
  5. Anno Siegel Guest

    Dr.Ruud <> wrote in comp.lang.perl.misc:
    > :
    >
    > > I have a variable which contains a text string. I want to print a
    > > modified verion of the string without changing the variable. Consider
    > > this code, which simply reverses each occurance of the word "gas":
    > >
    > > my $word = 'gas';
    > > my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
    > > (my $temp = $string) =~ s/$word/reverse $word/ieg;
    > > print "$temp\n";
    > >
    > > That works, but it uses an intermediate variable ($temp). I dislike
    > > intermediate variables.

    >
    > How do you feel about using '$_'?
    >
    > $_ = $string;
    > s/$word/reverse $word/ieg;
    > print $_, "\n";


    Squeezing it in a one-liner:

    print do { s/$word/reverse $word/ieg; $_ }, "\n" for "$string";

    The quotes around the bare variable "$string" are needed to protect $string
    from being changed in the process. Without them, $_ would be an alias for
    $string and s/// would apply to it.

    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.
    Anno Siegel, Oct 8, 2005
    #5
  6. Dr.Ruud Guest

    Anno Siegel:

    > Squeezing it in a one-liner:
    > print do { s/$word/reverse $word/ieg; $_ }, "\n" for "$string";



    I had been playing with these:

    print do {$_=$string; s/$word/reverse $word/ieg; $_}, "\n";


    print eval($_=$string, s/$word/reverse $word/ieg, '$_'), "\n";

    print ''.($_=$string, s/$word/reverse $word/ieg, $_)."\n";

    print ''.($_=$string, s/$word/reverse $word/ieg)[0], "\n";

    As you can see, I dont know how to stop 'print' to be a function.

    --
    Affijn, Ruud <http://www.pandora.com/?sc=sh770781&cmd=tunermini>

    "Gewoon is een tijger."
    Dr.Ruud, Oct 8, 2005
    #6
  7. Dr.Ruud <> wrote:


    > print eval($_=$string, s/$word/reverse $word/ieg, '$_'), "\n";
    >
    > print ''.($_=$string, s/$word/reverse $word/ieg, $_)."\n";
    >
    > print ''.($_=$string, s/$word/reverse $word/ieg)[0], "\n";
    >
    > As you can see, I dont know how to stop 'print' to be a function.



    I think this post should clear it up:

    Message-Id: <>


    IOW, simply choose to put parenthesis around print's argument list
    when you have a parenthesis following the function name.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 8, 2005
    #7
  8. Dr.Ruud Guest

    Tad McClellan:
    > Dr.Ruud:


    >> print ''.($_=$string, s/$word/reverse $word/ieg)[0], "\n";
    >> As you can see, I dont know how to stop 'print' to be a function.

    >
    > I think this post should clear it up:
    >
    > Message-Id:


    The Internet standard way to express such a pointer:

    <news:>

    (see RFC 3986)


    > IOW, simply choose to put parenthesis around print's argument list
    > when you have a parenthesis following the function name.


    Yes, I even knew the FAQ, but I hadn't tried it yet. I had even tried
    the "+" but couldn't get it to work.

    perldoc -f print says: Also be
    careful not to follow the print keyword with a left parenthesis
    unless you want the corresponding right parenthesis to termi-
    nate the arguments to the print--interpose a "+" or put paren-
    theses around all the arguments.

    OK, now I tried again. This gives the requested output:

    print (($_=$string, s/$word/reverse $word/ieg)[0], "\n");

    and this too:

    print +($_=$string, s/$word/reverse $word/ieg)[0], "\n";

    --
    Affijn, Ruud <http://www.pandora.com/?sc=sh770781&cmd=tunermini>

    "Gewoon is een tijger."
    Dr.Ruud, Oct 9, 2005
    #8
  9. Re: Print modified version of a string w/o intermediate variableor subroutine

    wrote:

    > my $word = 'gas';
    > my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
    > (my $temp = $string) =~ s/$word/reverse $word/ieg;
    > print "$temp\n";


    > I could create a subroutine, but that's even uglier.
    >
    > Is there an elegant way to print the modified version of the string
    > without an intermediate variable or a subroutine?


    That depends on what is you objection to a subroutine.

    If you just want to your code to look smooth and you are not worrying
    about the subroutine call overhead you could use List::MoreUtils::apply.

    use List::MoreUtils qw(apply);
    print ( (apply { s/$word/reverse $word/ieg } $string), "\n");
    Brian McCauley, Oct 10, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page