P
Phil Jacobson
Good afternoon everyone.
I'm trying to help a friend out with a client of his and I'm running
into an issue with Perl's s/// operator. We've got about 100 pages of
a simple shopping site. Each page has about 50-100 items on it.
Originally the customer wanted prices posted online, but now he's
decided he wants to have his customers call him for pricing. The
existing pages look like this:
<TR><TD>AEA3414AXAXA</TD>
<TD>1/6hp 115/1 R12</TD>
<TD><A HREF="/cgi-bin/addtocart?item=AEA3414AXAXA&page=cndu.htm">
<FONT COLOR="BLUE">$604.29</FONT></A></TD></TR>
<TR><TD>AEA3414YXAXA</TD>
<TD>1/6hp 115/1 R134A</TD>
<TD><A HREF="/cgi-bin/addtocart?item=AEA3414YXAXA&page=cndu.htm">
<FONT COLOR="BLUE">$638.57</FONT></A></TD></TR>
The first cell in each row is the part number. The second cell is the
description of the part and the third cell is a link containing the
price. The link will add the item to the user's shopping cart. I put
in some carriage returns to make it easier to read, but in the actual
HTML source, each table row is on one entire line with no carriage
returns. What I'd like to do is extract just the link and replace it
with "Call" which would look like this:
<TR><TD>AEA3414AXAXA</TD><TD>1/6hp 115/1 R12</TD><TD>Call</TD></TR>
<TR><TD>AEA3414YXAXA</TD><TD>1/6hp 115/1 R134A</TD><TD>Call</TD></TR>
Because of the amount of links on each individual page, I couldn't
just match the opening and closing <a> tag, I had to be more specific.
I loaded the existing page into an array (@file) and chomped it, then
I tried this code:
1. foreach my $line (@file)
2. {
3. if ($line =~ /(<a href="\/cgi-bin\/addtocart\?item=.+<\/a>)/i)
4. {
5. my $found = $1;
6. my $replacewith = "Call";
7. my $newline = $line;
8. $newline =~ s/$found/$replacewith/i;
9. print TO "$newline\n";
10. }
11. else
12. {
13. print TO "$line\n";
14. }
15. }
It didn't work. I ran the code again printing out the value in $found.
The value in $found contains the exact string I'm trying to replace,
but for some reason, the s/// operator won't do the swap. I started
tinkering with line 3 and I noticed something peculiar, this will
match and swap properly:
3. if ($line =~ /(<a href="\/cgi-bin\/addtocart\?)/i)
and this will match but not swap:
3. if ($line =~ /(<a href="\/cgi-bin\/addtocart\?item)/i)
Once I go past that question mark in the match, nothing swaps
properly. I've tried using all the modifiers in the manuals and I've
tried escaping and not escaping practically everything in the string.
We're using Perl 5.8.0 build 805 by Activestate on XP with SP1. Any
suggestions would be greatly appreciated. Thank you for your time.
Phil Jacobson
I'm trying to help a friend out with a client of his and I'm running
into an issue with Perl's s/// operator. We've got about 100 pages of
a simple shopping site. Each page has about 50-100 items on it.
Originally the customer wanted prices posted online, but now he's
decided he wants to have his customers call him for pricing. The
existing pages look like this:
<TR><TD>AEA3414AXAXA</TD>
<TD>1/6hp 115/1 R12</TD>
<TD><A HREF="/cgi-bin/addtocart?item=AEA3414AXAXA&page=cndu.htm">
<FONT COLOR="BLUE">$604.29</FONT></A></TD></TR>
<TR><TD>AEA3414YXAXA</TD>
<TD>1/6hp 115/1 R134A</TD>
<TD><A HREF="/cgi-bin/addtocart?item=AEA3414YXAXA&page=cndu.htm">
<FONT COLOR="BLUE">$638.57</FONT></A></TD></TR>
The first cell in each row is the part number. The second cell is the
description of the part and the third cell is a link containing the
price. The link will add the item to the user's shopping cart. I put
in some carriage returns to make it easier to read, but in the actual
HTML source, each table row is on one entire line with no carriage
returns. What I'd like to do is extract just the link and replace it
with "Call" which would look like this:
<TR><TD>AEA3414AXAXA</TD><TD>1/6hp 115/1 R12</TD><TD>Call</TD></TR>
<TR><TD>AEA3414YXAXA</TD><TD>1/6hp 115/1 R134A</TD><TD>Call</TD></TR>
Because of the amount of links on each individual page, I couldn't
just match the opening and closing <a> tag, I had to be more specific.
I loaded the existing page into an array (@file) and chomped it, then
I tried this code:
1. foreach my $line (@file)
2. {
3. if ($line =~ /(<a href="\/cgi-bin\/addtocart\?item=.+<\/a>)/i)
4. {
5. my $found = $1;
6. my $replacewith = "Call";
7. my $newline = $line;
8. $newline =~ s/$found/$replacewith/i;
9. print TO "$newline\n";
10. }
11. else
12. {
13. print TO "$line\n";
14. }
15. }
It didn't work. I ran the code again printing out the value in $found.
The value in $found contains the exact string I'm trying to replace,
but for some reason, the s/// operator won't do the swap. I started
tinkering with line 3 and I noticed something peculiar, this will
match and swap properly:
3. if ($line =~ /(<a href="\/cgi-bin\/addtocart\?)/i)
and this will match but not swap:
3. if ($line =~ /(<a href="\/cgi-bin\/addtocart\?item)/i)
Once I go past that question mark in the match, nothing swaps
properly. I've tried using all the modifiers in the manuals and I've
tried escaping and not escaping practically everything in the string.
We're using Perl 5.8.0 build 805 by Activestate on XP with SP1. Any
suggestions would be greatly appreciated. Thank you for your time.
Phil Jacobson