Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > find it, cut it out, find next

Reply
Thread Tools

find it, cut it out, find next

 
 
oldyork90
Guest
Posts: n/a
 
      01-16-2012
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.
 
Reply With Quote
 
 
 
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      01-16-2012
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
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      01-16-2012
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
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      01-17-2012
On Jan 15, 7:53*pm, oldyork90 <oldyor...@yahoo.com> 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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-19-2012
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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-22-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
Perl 5.00404 - Label not found for "next " , next() function Liora Perl Misc 5 01-12-2007 03:36 AM
Next (the other next) Gen "DVD" storage Alan Browne Digital Photography 13 05-31-2005 05:53 PM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM
Skipping content in Array within loop and go to the next (of NEXT function) Edward Wijaya Perl Misc 3 05-13-2004 03:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57