By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

Matching multiple strings from a file

P: 4
Hi, I am trying to match multiple strings per line from a file and extract them into an array. Apparently the first match is assigned $1, how do I know what the last match is ie $last?? I would like to set up a for loop to enter these values into an array: psuedo code below to extract all html tag per line in the file master, array is mastertags:

while (<$master>)
{
$_=~/(<.*?>)/g;
for (my $i=1; $i <= last value; $i++)
{
$mastertags [$x]=$i;
$x++;
}
}
Mar 19 '08 #1
Share this Question
Share on Google+
1 Reply


nithinpes
Expert 100+
P: 410
The value of $1 changes dynamically with each match found. In your case, though you are using /g option, you are not using a loop to parse through each match and end up getting only the first match in each line. You can try this:
Expand|Select|Wrap|Line Numbers
  1. my $m;
  2. while(<>) {
  3.   push @mastertags,$1 while(/(<.*?>)/g) ; ## push all the matches into array
  4.   $m=$1;      ## holds the string matched
  5. }
  6. print "last string matched:$m\n\n";  
  7. print "$_\n" foreach(@mastertags);
  8.  
Mar 19 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.