Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Don't ask to ask, just ask
 
PerlMonks  

(Challenge)(for new users) Replacing a text

by theroninwins (Friar)
on Sep 24, 2004 at 05:38 UTC ( [id://393430]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

theroninwins has asked for the wisdom of the Perl Monks concerning the following question:

Ok I found this little golf challenge on the net and would like to see how it can be solved.... then again i guess it will be fun for you all to try it.

Write a command-line Perl program to replace every occurance of 'Will Emigh' (exactly like that) with 'YAPH Emigh' in a given file. Hint: look at the -i command-line flag.

Update: thanks to castaway who told me that this here is not difficult I have to mention that this challenge was given by a University to test there best Perl users..... sorry about this I thought I would be more difficult.

the goal is to have the shortest answer.
  • Comment on (Challenge)(for new users) Replacing a text

Replies are listed 'Best First'.
Re: (Challenge)(for new users) Replacing a text
by robharper (Pilgrim) on Sep 24, 2004 at 07:01 UTC

    Well, I'm a newbie, so a rummage through perlrun (thanks for the hint) got me there. I'll hide this behind a readmore, just in case anyone else wants a go...

    Update: not including the filename at the end, 37 chars

      And with -w...

      # perl -wpi -e 's/Will( Emigh)/JAPH\1/g' FileName \1 better written as $1 at -e line 1. #

      antirice    
      The first rule of Perl club is - use Perl
      The
      ith rule of Perl club is - follow rule i - 1 for i > 1

Re: (Challenge)(for new users) Replacing a text
by tachyon (Chancellor) on Sep 24, 2004 at 07:02 UTC
    perl -pi -e 's/Will(?= Emigh)/YAPH/g' file

    cheers

    tachyon

Re: (Challenge)(for new users) Replacing a text
by theroninwins (Friar) on Sep 24, 2004 at 08:17 UTC
    In case you are interested in who has the shortest answer it would be nice if you could write the amount of chars used before the eradmore
Re: (Challenge)(for new users) Replacing a text
by Random_Walk (Prior) on Sep 24, 2004 at 08:18 UTC

    If the phrase split across a line break does not count then it is easy. If it does count then it is rather hard to do in a one liner without being clunky..

    # No worries about line breaks perl -i -pe's/Will Emigh/YAPH Emigh/g' # line breaks concern me but STDOUT will do perl -pe'$a.=$_}{$_=$a;s/Will(\n*) (\n*)Emigh/YAPH\1 \2Emigh/g' # line break concern me and lets put it back in the file perl -ne'BEGIN{$f=$ARGV[0]}$a.=$_}{$_=$a;s/Will(\n*) (\n*)Emigh/YAPH\1 + \2Emigh/g;open F,">$f";print F $_'
    Of course the last only works with one input file, and I got so worried about line breaks I forgot to make it short. (cunning tachyon)

    Cheers,
    R.

Re: (Challenge)(for new users) Replacing a text
by borisz (Canon) on Sep 24, 2004 at 10:02 UTC
    36 from me
    perl -i -pe's/Will(?= Emigh)/YAPH/g'
    Boris

      Does that include the surname in the changed version? Or am I missing something?

        Take a look! Just inspect the source of this page. My solution replace all occurrence of Will Emigh with YAPH Emigh.
        Boris
Re: (Challenge)(for new users) Replacing a text
by JediWizard (Deacon) on Sep 24, 2004 at 10:05 UTC
    perl -pi -e 's/Will(\s+Emigh)/YAPH$1/g'

    Is this what you are looking for?

    May the Force be with you

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://393430]
Approved by ysth
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.