regexp replace problem

Discussion in 'Perl Misc' started by Kasper, May 29, 2005.

  1. Kasper

    Kasper Guest

    Hello,
    I got a problem with regular expressions.

    My input data look like this :

    aaa,bbcc,bbdd,bbee
    aaa,bbcc,bbee,bbff,bbgg,bbhh

    (There could be many elements like (,[^,]*) in one line)

    I want to substitute every second and next element with first element.
    The result of presented lines should be :

    aaabbcc,aaabbdd,aaabbee
    aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh

    It don't have to be one line substitution.
    Please help - I have no idea how to do this and I tried almost everything.

    Kasper
    Kasper, May 29, 2005
    #1
    1. Advertising

  2. Kasper wrote:
    > I got a problem with regular expressions.
    >
    > My input data look like this :
    >
    > aaa,bbcc,bbdd,bbee
    > aaa,bbcc,bbee,bbff,bbgg,bbhh
    >
    > (There could be many elements like (,[^,]*) in one line)
    >
    > I want to substitute every second and next element with first element.
    > The result of presented lines should be :
    >
    > aaabbcc,aaabbdd,aaabbee
    > aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh
    >
    > It don't have to be one line substitution.


    Besides the simple regex used with the split() function, I wouldn't use
    regular expressions for this problem.

    perldoc -f split
    perldoc -f shift
    perldoc -f map
    perldoc -f join

    > Please help - I have no idea how to do this and I tried almost everything.


    Trying "almost everything", while having "no idea" how to do it, sounds
    utterly stupid.

    Please show us 10 or so of your attempts, and somebody may be able to
    help you fix your best shot.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 29, 2005
    #2
    1. Advertising

  3. * Kasper schrieb:
    >
    > My input data look like this :
    >
    > aaa,bbcc,bbdd,bbee
    > aaa,bbcc,bbee,bbff,bbgg,bbhh
    >
    > I want to substitute every second and next element with first element.
    > The result of presented lines should be :
    >
    > aaabbcc,aaabbdd,aaabbee
    > aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh
    >
    > It don't have to be one line substitution.


    This should be easy if you do it that way you would do it in mind. Split
    your string into an array, except the first element. Then put this first
    one in front of each remaining element. That's all.


    #!/usr/bin/perl -w
    use strict;
    while ( <DATA> ) {
    my( $first, @a ) = split /,/;
    print join ',', map $first.$_, @a;
    }
    __DATA__
    aaa,bbcc,bbdd,bbee
    aaa,bbcc,bbee,bbff,bbgg,bbhh


    regards,
    fabian
    Fabian Pilkowski, May 29, 2005
    #3
  4. Kasper <> wrote:

    > I got a problem with regular expressions.



    No you don't.

    Surely you can understand the /,/ regex, and that is all that is needed.


    > My input data look like this :
    >
    > aaa,bbcc,bbdd,bbee
    > aaa,bbcc,bbee,bbff,bbgg,bbhh


    > I want to substitute every second and next element with first element.



    "second and next" means the same thing as "second and third".

    You must have meant "second and subsequent" or some such?


    > The result of presented lines should be :
    >
    > aaabbcc,aaabbdd,aaabbee
    > aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh



    -------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    foreach ( 'aaa,bbcc,bbdd,bbee', 'aaa,bbcc,bbee,bbff,bbgg,bbhh' ) {
    my($prefix, @parts) = split /,/;
    print join( ',', map $prefix . $_, @parts), "\n";
    }
    -------------------



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 29, 2005
    #4
  5. Kasper

    Kasper Guest

    Hi
    Thanks for reply but I think that my post wasn't enough precise.
    I was thinking about only replace command.
    It is quite easy when first element is const and it's known (aaa)
    then s/,/,aaa/ is enough.
    I was thinking about gathering somehow the first element maybe like
    this (?=^[^,]+) and use it in replace for ",".
    Kasper
    Kasper, May 30, 2005
    #5
  6. * Kasper schrieb:
    >
    > Thanks for reply but I think that my post wasn't enough precise.
    > I was thinking about only replace command.
    > It is quite easy when first element is const and it's known (aaa)
    > then s/,/,aaa/ is enough.
    > I was thinking about gathering somehow the first element maybe like
    > this (?=^[^,]+) and use it in replace for ",".


    Please execute those examples from Tad and me (they're almost the same)
    to see what they do. They really do what you want. Assumedly, it is just
    one point you miss: the absence of a *complex* regular expression. But
    you don't need such one for this simple task, do you?

    Be happy and do this one without a regex.

    regards,
    fabian
    Fabian Pilkowski, May 30, 2005
    #6
    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. Sébastien Maurette
    Replies:
    3
    Views:
    129
    David Vallner
    Oct 2, 2006
  2. Greg Hurrell
    Replies:
    4
    Views:
    145
    James Edward Gray II
    Feb 14, 2007
  3. Mikel Lindsaar
    Replies:
    0
    Views:
    445
    Mikel Lindsaar
    Mar 31, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    328
    7stud --
    Aug 21, 2009
  5. Uldis  Bojars
    Replies:
    2
    Views:
    176
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page