On Mon, 19 Jan 2004, Jerry Preston wrote:
> I am trying to figure out a simple, Perl way to break down any sting similar
> to the following:
>
> $s0 =
> "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
> 26,27,28,29";
>
> Or in any numeric order. The string cannot be longer than 55 characters and
> end with ",".
>
> Ex:
>
> $s1 = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,";
> $s2 = "22,23,24,25,26,27,28,29"
>
> I know that I can use the following to start:
>
> if( length $s0 > 55 ) {
>
> $s1 = substr( $s0, 0, 54 );
> $s2 = substr( $s0, 54, length $s0 );
>
> }
Try this, I think it meets your criteria
Owen
----------------------------------------------------------
#!/usr/bin/perl -w
while (<DATA>){
chomp;
if (length > 55){next} #Meets the "not more than 55" condition
if (substr ($_, -1, length($_)) eq ","){next} #no comma on end of string
@single_numbers=split(/,/,$_);# now process the numbers into an array
foreach $nr(@single_numbers){
print "$nr\n";
}
}
__DATA__
01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,
01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27
22,23,24,25,26,27,28,29
122,123,124,125,126,127,128,129,
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>