CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
help with pattern matching/replacement please

 



amy_amy
Novice

Aug 13, 2006, 2:40 PM

Post #1 of 7 (1364 views)
help with pattern matching/replacement please Can't Post

Hi,

I would like to replace all the coords numbers (e.g decrease to 70%) in a image map file. I am wondering if it doable using Perl and how?

Note: Everything in the map file is in only one line (no delimiters).

<AREA SHAPE = "POLY" COORDS = " 150, 278, 164, 286, 155, 322, 145, 321, 152, 279" HREF = "a.asp">
<AREA SHAPE = "POLY" COORDS = " 511, 223, 521, 234, 519, 212, 508, 212, 506, 219, 499, 219, 499, 231, 521, 235" HREF = "a.asp">
<AREA SHAPE = "POLY" COORDS = " 544, 230, 553, 239, 552, 213, 540, 216, 539, 226, 532, 226, 533, 237, 552, 237" HREF = "a.asp">
<AREA SHAPE = "RECT" COORDS = " 459, 82, 502, 97" HREF = "a.asp">

Thanks in advance for your help.




rork
User

Aug 14, 2006, 3:52 AM

Post #2 of 7 (1358 views)
Re: [amy_amy] help with pattern matching/replacement please [In reply to] Can't Post

I'd try to substitute with s///e:


Code
  

s/(\d+)/$1*0,7/e



Sorry can't check it atm but I think that would work
--
Don't reinvent the wheel, use it, abuse it or hack it.


KevinR
Veteran


Aug 14, 2006, 11:52 AM

Post #3 of 7 (1351 views)
Re: [amy_amy] help with pattern matching/replacement please [In reply to] Can't Post

rork was close but this should do it, at least for the sample data you posted:


Code
while(<DATA>){ 
s/(\d+)/int($1*.7)/ge;
print;
}
__DATA__
<AREA SHAPE = "POLY" COORDS = " 150, 278, 164, 286, 155, 322, 145, 321, 152, 279" HREF = "a.asp">
<AREA SHAPE = "POLY" COORDS = " 511, 223, 521, 234, 519, 212, 508, 212, 506, 219, 499, 219, 499, 231, 521, 235" HREF = "a.asp">
<AREA SHAPE = "POLY" COORDS = " 544, 230, 553, 239, 552, 213, 540, 216, 539, 226, 532, 226, 533, 237, 552, 237" HREF = "a.asp">
<AREA SHAPE = "RECT" COORDS = " 459, 82, 502, 97" HREF = "a.asp">


the output is:


Code
<area SHAPE =" POLY" COORDS ="  105, 194, 114, 200, 108, 225, 101, 224, 106, 195" HREF =" a.asp"> 
<area SHAPE =" POLY" COORDS =" 357, 156, 364, 163, 363, 148, 355, 148, 354, 153, 349, 153, 349, 161, 364, 164" HREF =" a.asp">
<area SHAPE =" POLY" COORDS =" 380, 161, 387, 167, 386, 149, 378, 151, 377, 158, 372, 158, 373, 165, 386, 165" HREF =" a.asp">
<area SHAPE =" RECT" COORDS =" 321, 57, 351, 67" HREF =" a.asp">

-------------------------------------------------


amy_amy
Novice

Aug 14, 2006, 2:09 PM

Post #4 of 7 (1348 views)
Re: [KevinR] help with pattern matching/replacement please [In reply to] Can't Post

Kevin,

Thanks a lot. It worked!!!

But how can I only change the coordinates of the map and not the digits in a parameter name?

<AREA SHAPE = "RECT" COORDS = " 518, 175, 542, 200" HREF = "a.asp?name=Alpha11,Alpha12,Alpha13,Alpha14,Alpha15,AlphaQ,AlphaS">
<AREA SHAPE = "RECT" COORDS = " 303, 296, 343, 332" HREF = "a.asp?name=F1,F2">


(This post was edited by amy_amy on Aug 14, 2006, 2:46 PM)


KevinR
Veteran


Aug 14, 2006, 11:13 PM

Post #5 of 7 (1339 views)
Re: [amy_amy] help with pattern matching/replacement please [In reply to] Can't Post

now how did I know there was going to be a 'but' in there? ;)


Code
while(<DATA>){ 
s/COORDS\s*=\s*"([^"]+)"/'COORDS = "' . join(', ',map{int($_*.7)}split(',',$1)) . '"'/e;
print;
}
__DATA__
<AREA SHAPE = "RECT" COORDS = " 100, 175, 542, 200" HREF = "a.asp?name=Alpha11,Alpha12,Alpha13,Alpha14,Alpha15,AlphaQ,AlphaS">
<AREA SHAPE = "RECT" COORDS = " 303, 296, 343, 332" HREF = "a.asp?name=F1,F2">


see how easy that was? :)

There might be a better or more efficient way of doing this but that's what I came up with.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Aug 15, 2006, 4:06 AM

Post #6 of 7 (1336 views)
Re: [amy_amy] help with pattern matching/replacement please [In reply to] Can't Post

See, this is a perfect example of why you shouldn't try to parse HTML with regular expressions. KevinR has given you a solution, but each time you add another requirement like this, the regex gets more complex (and, hence, harder to get right).

In my opinion, the best solution is to use an HTML parser. In my solution below, I've used HTML::Parser itself rather than one of its many subclasses.


Code
use strict; 
use warnings;

use HTML::Parser;

my $html = do { local $/; <DATA> };

# Set up a parser that looks at start tags
# Everything else is passed through untouched
my $p = HTML::Parser->new(start_h => [ filter => 'tagname,attr,attrseq,text' ],
default_h => [ sub { print @_ }, 'text' ]);

$p->parse($html);

# Filter start tags
sub filter {
my ($tag, $attr, $attrseq, $text) = @_;

# If it's an 'area' tag
if ($tag eq 'area') {
# Update the 'coords' attribute
$attr->{coords} =~ s/(\d+)/int($1*.7)/ge;
# And rebuild the tag
print '<', uc $tag;
foreach (@$attrseq) {
print ' ', uc $_, ' = "';
print $attr->{$_};
print '"';
}
print '>';
} else {
# Otherwise just print the existing text
print $text;
}
}

__DATA__
<AREA SHAPE = "RECT" COORDS = " 518, 175, 542, 200" HREF = "a.asp?name=Alpha11,Alpha12,Alpha13,Alpha14,Alpha15,AlphaQ,AlphaS">
<AREA SHAPE = "RECT" COORDS = " 303, 296, 343, 332" HREF = "a.asp?name=F1,F2">


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Aug 15, 2006, 9:05 AM

Post #7 of 7 (1333 views)
Re: [davorg] help with pattern matching/replacement please [In reply to] Can't Post


Quote
In my opinion, the best solution is to use an HTML parser. In my solution below, I've used HTML::Parser itself rather than one of its many subclasses.


I agree, it is the best solution.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives