[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Need better way to skin a cat



I'm working with some text files that have 
style tags like this <styletag>. I want to 
write a brief Perl statement that removes 
"<>" and any text between the greater and 
lesser than symbols. In these text file the 
symbols exist in matching pairs.

My first hack is just too wordy. It does this:

1. I find the < in each line then copy everything befor it to $Before
2. I find the > in each line then copy everything after it to $After
3. I print $Before and $After (voila! the <styletag> is gone).

One problem is that my method only works 
when there's only ONE <styletag> per line. 

Is a way to get Perl to match <*> (the symbols 
and everything between) and delete? I prefer a 
wildcard searh because there are many, many 
<styletags> to deal with. I don't want to require 
exact name matches, just <wildcardtag> matches.

My current working (but clunky) code is below. 
Does anyone feel like offering an improvement?

Thanks.

Eric

========== code ==========

sub DeleteStuff {
  for ($i=0;$i<$#Lines+1;$i++) {
    $Lines[$i] =~ m/</;  #matches <
    $Before[$i] = "$`";  #copies everything before < into $Before
    $Lines[$i] =~ m/>/;  #matches >
    $After[$i] = "$'";   #copies everything after  > into $After
  }
}

sub ReturnDoc {
  for ($i=1;$i<$#Lines+1;$i++) {
    print "$Before[$i]$After[$i]";
  }
}