substitution with computed replacements

Discussion in 'Perl Misc' started by fred, Aug 3, 2009.

  1. fred

    fred Guest

    I took this from Minimal perl by Tim Maher.

    s/\d+ /$& * 1.6/ge

    I am trying to add numbers instead of converting miles to KM.

    input
    2 3 4 1
    3 4 6

    output
    2 3 4 1 = 10
    2 4 6 = 12

    1. What would the replacement part be ?
    2. What should the seach part be if we have ten numbers instead of
    four?

    Thanks
     
    fred, Aug 3, 2009
    #1
    1. Advertising

  2. fred

    kappa Guest

    On 3 Aug., 16:57, fred <> wrote:
    > I took this from Minimal perl by Tim Maher.
    >
    > s/\d+ /$& * 1.6/ge
    >
    > I am trying to add numbers instead of converting miles to KM.
    >
    > input
    > 2 3 4 1
    > 3 4 6
    >
    > output
    > 2 3 4 1 = 10
    > 2 4 6 = 12
    >
    > 1. What would the replacement part be ?
    > 2. What should the seach part be if we have ten numbers instead of
    > four?
    >
    > Thanks


    If you want to practice regexp stuff:

    kappa@kappa:~$ head -2 /etc/hosts | perl -lne 'use List::Util "sum";
    @a = /(\d+)/g; print "@a = ",sum @a'
    127 0 0 1 = 128
    127 0 1 1 = 129
     
    kappa, Aug 9, 2009
    #2
    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.
Similar Threads
  1. Peter Bengtsson

    Squezing in replacements into strings

    Peter Bengtsson, Apr 25, 2005, in forum: Python
    Replies:
    3
    Views:
    318
    Peter Bengtsson
    Apr 25, 2005
  2. Claude Henchoz

    URL 'special character' replacements

    Claude Henchoz, Jan 9, 2006, in forum: Python
    Replies:
    6
    Views:
    643
    Claude Henchoz
    Jan 9, 2006
  3. Brian McCullough
    Replies:
    0
    Views:
    500
    Brian McCullough
    Feb 16, 2007
  4. Antoine De Groote
    Replies:
    10
    Views:
    443
    Duncan Booth
    Oct 25, 2006
  5. Replies:
    10
    Views:
    543
    Robert Kern
    Apr 9, 2008
Loading...

Share This Page