469,279 Members | 2,200 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,279 developers. It's quick & easy.

Matching multiple strings from a file

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
1 1452
nithinpes
410 Expert 256MB
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.

Similar topics

9 posts views Thread by Xah Lee | last post: by
3 posts views Thread by Day Of The Eagle | last post: by
5 posts views Thread by Jamie Jackson | last post: by
10 posts views Thread by javuchi | last post: by
5 posts views Thread by olaufr | last post: by
7 posts views Thread by Captain Dondo | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.