For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > expanding Ranges









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author expanding Ranges
N. Ganesh Babu

2005-04-24, 3:55 pm

Dear All,

input:

<aud=1-3>T
<cpn=1-3,4,5,6-10>6
<bnd=1-3>TC

output

<aud=1,2,3>T
<cpn=1,2,3,4,5,6,7,8,9,10>6
<bnd=1,2,3>TC

How can it be achieved in regular expression?. Or any other means is
easy. I am trying to put the coding of for loop inside substitution
string with (?{...}) but it is not working.


Please help.

Regards,
Ganesh

Randy W. Sims

2005-04-24, 3:55 pm

N. Ganesh Babu wrote:
> Dear All,
>
> input:
>
> <aud=1-3>T
> <cpn=1-3,4,5,6-10>6
> <bnd=1-3>TC
>
> output
>
> <aud=1,2,3>T
> <cpn=1,2,3,4,5,6,7,8,9,10>6
> <bnd=1,2,3>TC
>
> How can it be achieved in regular expression?. Or any other means is
> easy.


use Set::IntSpan;

my $set = Set::IntSpan->new('1-3,4,5,6-10');
my @elems = $set->elements;
print join( ',', @elems ), "\n";

__END__

John W. Krahn

2005-04-24, 8:55 pm

N. Ganesh Babu wrote:
> Dear All,


Hello,

> input:
>
> <aud=1-3>T
> <cpn=1-3,4,5,6-10>6
> <bnd=1-3>TC
>
> output
>
> <aud=1,2,3>T
> <cpn=1,2,3,4,5,6,7,8,9,10>6
> <bnd=1,2,3>TC
>
> How can it be achieved in regular expression?. Or any other means is
> easy. I am trying to put the coding of for loop inside substitution
> string with (?{...}) but it is not working.


$ perl -le'
$_ = q/<cpn=1-3,4,5,6-10>6/;
print;
s/(\d+)-(\d+)/join ",", $1 .. $2/eg;
print;
'
<cpn=1-3,4,5,6-10>6
<cpn=1,2,3,4,5,6,7,8,9,10>6




John
--
use Perl;
program
fulfillment
N. Ganesh Babu

2005-04-25, 3:56 am

Thanks John,

I did not think this is this much easy.

Thanks alot.

Regards,
Ganesh


John W. Krahn wrote:

> N. Ganesh Babu wrote:
>
>
>
> Hello,
>
>
>
> $ perl -le'
> $_ = q/<cpn=1-3,4,5,6-10>6/;
> print;
> s/(\d+)-(\d+)/join ",", $1 .. $2/eg;
> print;
> '
> <cpn=1-3,4,5,6-10>6
> <cpn=1,2,3,4,5,6,7,8,9,10>6
>
>
>
>
> John


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com