find it, cut it out, find next

Discussion in 'Perl Misc' started by oldyork90, Jan 16, 2012.

  1. oldyork90

    oldyork90 Guest

    I have a string that contains numbers and ranges of numbers, like

    '1 2 4-6 8 20 - 23' which translates as "include numbers 1, 2, 4, 5,
    6, 8, 20, 21, 22, 23'

    I can find the range "objects" with

    while ($s =~ /(\d+ *- *\d+)/g) {
    # work with $1
    }

    Is there a way to trim up this string during the loop so that when
    it's done, the range "objects" are gone and I'm left with the discreet
    list of digits. It would look like this when complete '1 2 8'

    I can trim it up later with s/(\d+ *- *\d+)//g, but was wondering if
    this could be a one step thing. Seems like it would be a common
    operation... find it, cut it out, find the next. I don't want
    anything complex... just wondering.

    Thank you.
    oldyork90, Jan 16, 2012
    #1
    1. Advertising

  2. Am 16.01.2012 04:53, schrieb oldyork90:
    > I have a string that contains numbers and ranges of numbers, like
    >
    > '1 2 4-6 8 20 - 23' which translates as "include numbers 1, 2, 4, 5,
    > 6, 8, 20, 21, 22, 23'
    >
    > I can find the range "objects" with
    >
    > while ($s =~ /(\d+ *- *\d+)/g) {
    > # work with $1
    > }
    >
    > Is there a way to trim up this string during the loop so that when
    > it's done, the range "objects" are gone and I'm left with the discreet
    > list of digits. It would look like this when complete '1 2 8'


    Sure, just get rid of the /g (you have the while loop):

    perl -E'
    $s="1 2 4-6 8 20 - 23";
    while($s =~ s/(\d+) *- *(\d+)//) {
    say "work with range $1 to $2"
    }
    say "rest=$s"
    '

    - Wolf
    Wolf Behrenhoff, Jan 16, 2012
    #2
    1. Advertising

  3. oldyork90

    Willem Guest

    oldyork90 wrote:
    ) I have a string that contains numbers and ranges of numbers, like
    )
    ) '1 2 4-6 8 20 - 23' which translates as "include numbers 1, 2, 4, 5,
    ) 6, 8, 20, 21, 22, 23'
    )
    ) I can find the range "objects" with
    )
    ) while ($s =~ /(\d+ *- *\d+)/g) {
    ) # work with $1
    ) }
    )
    ) Is there a way to trim up this string during the loop so that when
    ) it's done, the range "objects" are gone and I'm left with the discreet
    ) list of digits. It would look like this when complete '1 2 8'

    This sounds a hell of a lot like an X-Y problem.

    Do you really want to do one thing with the ranges, and something different
    with the single numbers? Or do you want to do the same thing with each
    included number, but you've already figured out that you can first do the
    while loop you wrote above, and then a simple loop?

    Maybe you can get an idea from this piece of code, whatever you want to do:

    $s =~ s/(\d+) *- *(\d+)/join(' ',($1 .. $2))/ge;

    ) I can trim it up later with s/(\d+ *- *\d+)//g, but was wondering if
    ) this could be a one step thing. Seems like it would be a common
    ) operation... find it, cut it out, find the next. I don't want
    ) anything complex... just wondering.

    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Jan 16, 2012
    #3
  4. oldyork90

    C.DeRykus Guest

    On Jan 15, 7:53 pm, oldyork90 <> wrote:
    > I have a string that contains numbers and ranges of numbers, like
    >
    > '1 2 4-6 8 20 - 23'  which translates as "include numbers 1, 2, 4, 5,
    > 6, 8, 20, 21, 22, 23'
    >
    > I can find the range "objects" with
    >
    > while ($s =~ /(\d+ *- *\d+)/g) {
    >   # work with $1
    >
    > }
    >
    > Is there a way to trim up this string during the loop so that when
    > it's done, the range "objects" are gone and I'm left with the discreet
    > list of digits.  It would look like this when complete '1 2 8'
    >
    > I can trim it up later with s/(\d+ *- *\d+)//g, but was wondering if
    > this could be a one step thing.  Seems like it would be a common
    > operation... find it, cut it out, find the next.  I don't want
    > anything complex... just wondering.


    $re =qr/(\d\s* (-) \s*\d+ | \d+) /x;
    while ($s=~/$re/g ) {
    $p=$1;
    $s=~s/$p// if defined $2;
    }

    --
    Charles DeRykus
    C.DeRykus, Jan 17, 2012
    #4
  5. oldyork90

    Dr.Ruud Guest

    On 2012-01-16 04:53, oldyork90 wrote:

    > I have a string that contains numbers and ranges of numbers, like
    >
    > '1 2 4-6 8 20 - 23' which translates as "include numbers 1, 2, 4, 5,
    > 6, 8, 20, 21, 22, 23'


    perl -Mstrict -wle '
    my $data = "1 2 4-6 8 20 - 23 99-100";

    my @data = map { s/\s+\z//, s/\A\s+//; length() ? $_ : () }
    split /([^0-9]+)/, $data;

    for my $i ( 0 .. $#data ) {
    splice @data, $i, 1,
    ( $data[ $i - 1 ] + 1 .. $data[ $i + 1 ] - 1 )
    if $data[ $i ] eq "-";
    }

    print "include numbers ", join ", ", @data;
    '
    include numbers 1, 2, 4, 5, 6, 8, 20, 21, 22, 23, 99, 100

    --
    Ruud
    Dr.Ruud, Jan 19, 2012
    #5
  6. Dr.Ruud wrote:
    > On 2012-01-16 04:53, oldyork90 wrote:
    >
    >> I have a string that contains numbers and ranges of numbers, like
    >>
    >> '1 2 4-6 8 20 - 23' which translates as "include numbers 1, 2, 4, 5,
    >> 6, 8, 20, 21, 22, 23'

    >
    > perl -Mstrict -wle '
    > my $data = "1 2 4-6 8 20 - 23 99-100";
    >
    > my @data = map { s/\s+\z//, s/\A\s+//; length() ? $_ : () }
    > split /([^0-9]+)/, $data;


    Or just:

    my @data = $data =~ /[0-9]+|-/g


    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
    John W. Krahn, Jan 22, 2012
    #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. John Salerno
    Replies:
    7
    Views:
    399
    Dennis Lee Bieber
    May 18, 2006
  2. Deniz Bahar
    Replies:
    2
    Views:
    443
    Andrey Tarasevich
    Mar 9, 2005
  3. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    768
    Alf P. Steinbach
    Jul 10, 2007
  4. Florian Lindner

    Cut out XML subtree

    Florian Lindner, Aug 29, 2012, in forum: Python
    Replies:
    0
    Views:
    172
    Florian Lindner
    Aug 29, 2012
  5. Andreas Perstinger

    Re: Cut out XML subtree

    Andreas Perstinger, Aug 29, 2012, in forum: Python
    Replies:
    0
    Views:
    188
    Andreas Perstinger
    Aug 29, 2012
Loading...

Share This Page