I had a quick-and-dirty script that worked with the substring function as long as the offset and length of text were the same. This is no longer true. Now the length of text to be extracted is variable (and sometimes contains spaces).
I can determine the text around the substring. Let's say start and end.
I thought I could more generically extract the substring with split /start($_)end/ but I can't get the syntax right and it won't compile.
Here's the original script. It works, finding one $transactID and a dozen or so item/quantity instances in each file, but it's inflexible:
#!/usr/bin/perl -w use strict; use warnings; my $transactID = "item=" ; #### Usually a number my $itemname = "itemname" ; #### Usually alphanumeric my $quantity = "qty" ; #### Always numeric open(IN,'ItemsFile.rtf') or die("can't open input file\n"); open(OUT,'>results.txt') or die("can't open output file\n"); while(<IN>){ print OUT "0 ", substr($',3,17), "; " if($_ =~/\b$transactID\b/i); print OUT "1 ", substr($',2,12), "; " if($_ =~/\b$itemID\b/i); print OUT "2 ", substr($',13,3), "; " if($_ =~/\b$quantity\b/i); + } close(IN); close(OUT);
Note that start sometimes contains spaces.
In reply to Extracting variable-length strings between delimiters by PMReader
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |