""Christopher Gray"" wrote in message news
Good day,
I have a text file containing records. While I can extract single
sub-strings, I cannot extract multiple sub-strings.
The records are of multiple types - only about a third of which have the
data I need.
An example of a "good" record is
Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { fruittype 1}
I'm trying to extract the first (Abc1234), second (open), third (A basket
of
melons) and fourth (1) strings.
I can extract each of them separately - but not together.
So - for example:
while (<FILE>) {
chomp;
next if !/\{\s+fruittype\s+(\d+)\s+}/;
my $Temp =$1;
}
Extracts the fruittype. However, when I try and have multiple extracts:
...
next if !/\STATUS\s+(\w+)\s+\{\s+fruittype\s+(\d+)\s+}/;
...
It fails.
What have I done wrong?
Chris
Hello Chris
Just to show another way to do it, using Text::ParseWords which has been
part of the core since perl 5. (To use only if file format is consistent.)
#!/usr/bin/perl
use strict;
use warnings;
use Text::ParseWords;
my @data;
while (<DATA>) {
chomp;
# use substitution operator to remove the end of the line
next unless s/\s*::\s*\{\s*fruittype\s*(\d+).+//;
my $fruittype = $1;
push @data, { 'id', quotewords('\s+', 0, $_), type => $fruittype };
}
use Data::Dumper; print Dumper \@data;
__DATA__
Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { fruittype 1}
*** Dumper prints
$VAR1 = [
{
'DESCRIPTION' => 'A basket of melons',
'STATUS' => 'open',
'type' => '1',
'id' => 'Abc1234'
}
];
Hope this helps,
Chris
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/