find it, cut it out, find next

O

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'

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.
 
W

Wolf Behrenhoff

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
 
W

Willem

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
 
C

C.DeRykus

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;
}
 
D

Dr.Ruud

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
 
J

John W. Krahn

Dr.Ruud said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top