Hi Chris, On Sat, May 26, 2012 at 8:14 AM, Christopher Gray < christopher.g...@talktalk.net> wrote:
> Hi, > > Thank you for looking to help me. Unfortunately, when I used your code > nothing was extracted. At least when I printed $1 nothing appeared. > > In order to help me learn more about Perl - I'd like to know why my code > didn't work. If I understand your code correctly then I should see: > > $1 = Abc123, $2 = STATUS, $3 = open, $3 = DESCRIPTION, etc. > > While anything would be better than my puny effort, I was looking just to > extract the "data" - in other words: > > $1 = "Abc123", $2 = "open", $3 = "a basket of melons", $4 = "1". > > I thought I was successfully copying from a similar script - but obviously > I don't have enough understanding in order to make it work. If I take my > code apart, I thought that: > > \STATUS\s+(\w+)\s+ would extract the word between STATUS and > DESCRIPTION, and > \{\s+fruittype\s+(\d+)\s+} would extract the number following > "fruittype" > > Individually they do - it is just when I add then together in the same > line that I receive nothing. What am I doing wrong? > > Chris > > - > You gave this as a type of good record: Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { fruittype 1} Let me include the line below as a type of incorrect record, That is if "fruittype" is common to all correct record: Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { 1} Case1: say we are to match to get Abc 1234, one can do this: while(<DATA>){ next unless m/(.+?)\s+?.+?\{\s+?fruittype.+?\}$/; print $1,$/ # prints Abc1234 for the first line } __DATA__ Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { fruittype 1} Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { 1} Case 2: say again we want to match 'A basket of melons', at a different times we can do this: while(<DATA>){ next unless m/.+?\"(.+?)\".+?\{\s+?fruittype.+?\}$/; print $1,$/ # prints A basket of melons } __DATA__ Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { fruittype 1} Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { 1} Let say we want to output both 'Abc1234' and 'A basket of melons', then we need to merge the two regex togther like so: .......... next unless m/(.+?)\s+?.+?\"(.+?)\".+?\{\s+?fruittype.+?\}$/; print "$1 $2",$/ # prints Abc1234 A basket of melons ......... We can't use either of those previous regex, it won't match. In that light to get what you wanted i.e $1 = "Abc123", $2 = "open", $3 = "a basket of melons", $4 = "1". use this: while(<DATA>) { next unless m/(.+?)\s+?.+?\s.+?(.+?)\s+?.+?\"(.+?)\".+?\{\s+fruittype\s+(.+?)\}/; print "$1 $2 $3 $4\n"; # prints Abc1234 open A basket of melons 1 } __DATA__ Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { fruittype 1} Abc1234 STATUS open DESCRIPTION "A basket of melons" :: { 1} Pls Note that the second record will not match atall because it doesn't have fruittype. Copy the code in the cases out and test them, yourself it works. Hope this helps