I am writing a prel program in which I have an input file containing a pattern as:
FIELDS=(1,2,3,4)
OR
FIELDS=(1,10,3,A,11,10,7
You can use split to break you comma separated matches into individual pieces:
use strict;
use warnings;
my $line = 'FIELDS=(1,10,3,A,11,10,7,D,9,10,11,A)'
if ( my ($fields) = $line =~ /FIELDS=(\(.*\))/ ) {
my @vars = split /,/, $fields;
# do whatever you want with @vars
}
TIMTOWTDI. There is one of them:
use strict;
use warnings;
while (<DATA>) {
next unless /^FIELDS=\(([^)]*)\)/;
my @fields = split ',', $1;
while (@fields) {
my @subfields = splice @fields, 0, 4;
print "$. @subfields\n";
}
}
__DATA__
FIELDS=(1,2,3,4)
FIELDS=(1,10,3,A,11,10,7,D,9,10,11,A)
You can use split, like this
use strict;
use warnings;
my $str = 'FIELDS=(1,10,3,A,11,10,7,D,9,10,11,A)';
if ( $str =~ / FIELDS \s* = \s* \( ( [^)]* ) \) /x ) {
my @fields = split /,/, $1;
print "$_\n" for @fields;
}
output
1
10
3
A
11
10
7
D
9
10
11
A