Need help on building regex

D

doni

Hi all,

I'm new to perl & also new to regex. Could u all please help me
building a regex to do the following:

I have text files with lines like these:
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f

I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
e, f are numerics.

Any help will be much appreciated.
 
T

tfe

Hello,

if the numbers are at the end of each line , you can use: the regular
expression /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/

you will get ($1,$2,$3,$3,$4,$5,$6) containing a b c d e f.


doni a écrit :
 
M

Mirco Wahab

doni said:
I'm new to perl & also new to regex. Could u all please help me
building a regex to do the following:

I have text files with lines like these:
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f

I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
e, f are numerics.

You wont need a regex for fixed-format stuff
like that, a simple 'split' will do.

You'll need to learn:
- 1) open a file
- 2) read from the file
- 3) split the line read into fields
- 3) put the fields into an array

Here's one Idea (try to figure out the steps):

---8<----

use strict;
use warnings;

my $text = q'
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
';

my @array;

open(my $fh, '<', \$text) or die "without a blink $!";
while( <$fh> ) {
push @array, [ (split) [5..10] ];
}
close $fh;

for my $row (@array) {
print join ',', @$row;
print "\n";
}
---8<----

Regards

Mirco
 
D

Dr.Ruud

doni schreef:
I have text files with lines like these:
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f

I want to read the value of a, b, c, d, e, f into an array.
a, b, c, d, e, f are numerics.

perl -wnle '
$, = "," ;
print +(/[0-9]+/g)[-6..-1]
' filename
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top