CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
parsing a line of numbers

 



piper68
Novice

Mar 14, 2010, 10:57 AM

Post #1 of 7 (4420 views)
parsing a line of numbers Can't Post

I have a file with the contents of this....

UTC 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

what i would like to do is parse each set of numbers with a comma and each on a new line so like this...

15,
16,
17,
18,
19,
20,
21,
22,
23,
00,
01,
02,
03,
04,
05,
06,
07,
08,
09,
10,
11,
12,
13,
14,
15,

i'm trying to learn perl so any help on this would be greatly appreciated.

thanks


FishMonger
Veteran / Moderator

Mar 14, 2010, 12:43 PM

Post #2 of 7 (4417 views)
Re: [piper68] parsing a line of numbers [In reply to] Can't Post

What have you tried?


piper68
Novice

Mar 14, 2010, 1:06 PM

Post #3 of 7 (4415 views)
Re: [FishMonger] parsing a line of numbers [In reply to] Can't Post

I've tried this and works but is there a way to write this more simple? thanks.

perl -pe '/UTC[\s]+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;$_=($1)?"$1,\n$2,\n$3,\n$4,\n$5,\n$6,\n$7,\n$8,\n$9,\n$10,\n$11,\n$12,\n$13,\n$14,\n$15,\n$16,\n$17,\n$18,\n$19,\n$20,\n$21,\n$22,\n$23,\n$24,":""' $TXT_PATH/a.txt > $TXT_PATH/b.txt


(This post was edited by piper68 on Mar 14, 2010, 1:07 PM)


FishMonger
Veteran / Moderator

Mar 14, 2010, 1:31 PM

Post #4 of 7 (4411 views)
Re: [piper68] parsing a line of numbers [In reply to] Can't Post


Code
perl -ane 'for(@F){print "$_,\n" unless /\D/}' a.txt



murugaperumal
Novice


Mar 14, 2010, 10:49 PM

Post #5 of 7 (4404 views)
Re: [piper68] parsing a line of numbers [In reply to] Can't Post

Use the following code

Code
my $var="UTC 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 "; 
my @array;
@array=split(" ",$var);
foreach(@array)
{
if(/\d+/)
{
print "$_,\n";
}
}



(This post was edited by murugaperumal on Mar 14, 2010, 10:49 PM)


shawnhcorey
Enthusiast


Mar 15, 2010, 7:19 AM

Post #6 of 7 (4379 views)
Re: [piper68] parsing a line of numbers [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

while( <DATA> ){
my @captured = m{ (\d+) }gmsx;
print join( ",\n", @captured ), ",\n";
}

__DATA__
UTC 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


roolic
User

Mar 22, 2010, 11:05 PM

Post #7 of 7 (4199 views)
Re: [piper68] parsing a line of numbers [In reply to] Can't Post

using the regular expression:


Code
my $str = "UTC 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15"; 

# first remove non-digital symbols from the line start
$str =~ s/^\D+//;

# actually reformat
$str =~ s/(\d+)\D*/$1,\n/g;

print $str;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives