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: Regular Expressions:
Split Function acting up

 



perlkid
stranger

Aug 2, 2000, 7:39 PM

Post #1 of 15 (8135 views)
Split Function acting up Can't Post

 
Why does this code

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


foreach $result(@results){
($tmp, $pic) = split(/<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>/i, $result);
($pic, $tmp) = split(/<\/TD>/i, $pic);
($ttp, $info) = split(/<TD WIDTH=100 VALIGN="top">/i, $result);
($info, $ttp) = split(/<\/TD>/i, $info);
print "$pic";
</pre><HR></BLOCKQUOTE>


Only print the first picture between the tags

<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
and
</TD>

Shouldn't it print the first picture between these tags, then the second, and so on.

Why, Please Help,

perlkid


dws
Deleted

Aug 2, 2000, 10:01 PM

Post #2 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

1. You didn't show us your input string(s).

2. You're using the wrong tool. This is probably much better done with a regular expression than with split.


perlkid
stranger

Aug 3, 2000, 11:54 AM

Post #3 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

 
You mean the html that's comming in?

If so here it is.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


<TABLE BORDER=0 BGCOLOR="#6098CF" CELLPADDING=0 CELLSPACING=0 WIDTH="440">
<TR><TD><FONT FACE="verdana,helvetica,arial" COLOR="#FFFFFF" SIZE=-1><B> Pictures of "acura" 1-10 of 507 </B></FONT></TD></TR>
</TABLE><TABLE BORDER=0 WIDTH=440>
<TR>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://viewimages.lycos.com/viewimage?imageid=111475&promotionid=28&partnerid=12&type=results">
<IMG WIDTH=90 HEIGHT=62 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=138158176" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
6.9 kB - 30-Apr-2000; 325x223<BR>
<B>111475</B><BR>
North American International Auto Show F362948 08: ( DETROIT PUBLICATIONS OUT, DETROIT FREE PRESS AN...<BR>
<BR>
</TD>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://viewimages.lycos.com/viewimage?imageid=NM013608&partnerid=12&promotionid=28">
<IMG WIDTH=90 HEIGHT=62 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=136945723" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
6.9 kB - 30-Apr-2000; 325x223<BR>
<B>NM013608</B><BR>
North American International Auto Show F362948 08: ( DETROIT PUBLICATIONS OUT, DETROIT FREE PRESS AN...<BR>
<BR>
</TD>
</TR>
<TR>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://home.mpinet.net/xtreme14u/acurah2o.jpg">
<IMG WIDTH=120 HEIGHT=90 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=3956989" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
299.4 kB - 15-Oct-1998; 800x600<BR>
<B>Acura Integra WallpaperAcura Integra WallpaperAcura Integra WallpaperAcura Integra WallpaperAcura Integra Wallpaper</B><BR>
xtreme u acurah o jpgAcura IntegraAcura IntegraAcura IntegraAcura IntegraAcura Integra<BR>
<A HREF="http://www3.winfiles.com/apps/98/wallpaper-vehicle.html"><FONT COLOR="#c80400">Link to page</FONT></A><BR>
<BR>
</TD>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://www.tsan.com/images/acura-images/slx.jpg">
<IMG WIDTH=120 HEIGHT=69 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=7675691" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
20.9 kB - 04-Dec-1998; 200x115<BR>
<B>slx.jpg</B><BR>
images acura images slx jpg<BR>
<A HREF="http://www.tsan.com/sunnyvale_acura/99models.html"><FONT COLOR="#c80400">Link to page</FONT></A><BR>
<BR>
</TD>
</TR>
<TR>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://www.auto.com/art/reviews/99_acura_3.2_tl/0918_acura_stryfrt.jpg">
<IMG WIDTH=120 HEIGHT=64 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=22813131" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
16.9 kB - 31-Oct-1998; 315x168<BR>
<B>0918_acura_stryfrt.jp...</B><BR>
art reviews acura tl acura stryfrt jpgThe new Acura is a half- inch narrower, but overall length has...<BR>
<A HREF="http://www.auto.com/reviews/det_swan_acura_0917.htm"><FONT COLOR="#c80400">Link to page</FONT></A><BR>
<BR>
</TD>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://www.auto.com/art/1997_acura_cl.jpg">
<IMG WIDTH=120 HEIGHT=77 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=14313965" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
13.0 kB - 03-Aug-1996; 270x174<BR>
<B>1997_acura_cl.jpg</B><BR>
art acura cl jpgAcura 2. 2 CL lacks power, but packs in comfy features By TONY<BR>
<A HREF="http://www.auto.com/reviews/swan_acura_22cl.htm"><FONT COLOR="#c80400">Link to page</FONT></A><BR>
<BR>
</TD>
</TR>
<TR>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://cars.whatodo.ru/mrk/Acura/98/Tl/Tl1.jpg">
<IMG WIDTH=120 HEIGHT=60 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=19326136" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
19.3 kB - 04-Feb-1998; 400x200<BR>
<B>Tl1.jpg</B><BR>
mrk Acura Tl Tl jpgTl Acura<BR>
<A HREF="http://cars.whatodo.ru/mrk/Acura/98/Tl/"><FONT COLOR="#c80400">Link to page</FONT></A><BR>
<BR>
</TD>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://cars.whatodo.ru/mrk/Acura/97/TL/tl.jpg">
<IMG WIDTH=120 HEIGHT=51 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=15824096" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
9.1 kB - 26-Nov-1997; 330x141<BR>
<B>tl.jpg</B><BR>
mrk Acura TL tl jpgTL Acura<BR>
<A HREF="http://cars.whatodo.ru/mrk/Acura/97/TL/"><FONT COLOR="#c80400">Link to page</FONT></A><BR>
<BR>
</TD>
</TR>
<TR>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://www.tsan.com/images/acura-images/fl_cl0.jpg">
<IMG WIDTH=120 HEIGHT=68 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=6385005" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
10.5 kB - 01-Feb-1998; 188x108<BR>
<B>fl_cl0.jpg</B><BR>
images acura images fl cl jpg<BR>
<A HREF="http://www.tsan.com/university_acura/98models.html"><FONT COLOR="#c80400">Link to page</FONT></A><BR>
<BR>
</TD>
<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>
<A HREF="http://www.tsan.com/images/acura-images/fl_integra0.jpg">
<IMG WIDTH=120 HEIGHT=66 BORDER=0 SRC="http://img.multimedia.lycos.com/thumbnail?cmd=key&param=7937647" ALT="thumbnail"></A>
</TD>
<TD WIDTH=100 VALIGN="top">
<FONT COLOR="#000000" FACE="verdana,helvetica,arial" SIZE="-2">
10.1 kB - 01-Feb-1998; 186x103<BR>
<B>fl_integra0.jpg</B><BR>
images acura images fl integra jpg<BR>
<A HREF="http://www.tsan.com/university_acura/98models.html"><FONT COLOR="#c80400">Link to page</FONT></A><BR>
<BR>
</TD>
</TR>
</TABLE>
</pre><HR></BLOCKQUOTE>


dws
Deleted

Aug 3, 2000, 12:21 PM

Post #4 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

That's a start. Now how are you setting up @results?


perlkid
stranger

Aug 3, 2000, 5:28 PM

Post #5 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

 What I did was this.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl
################################################
sub parse_form {
my (@pairs, %in);
my ($buffer, $pair, $name, $value);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&cgierr('You cant run this script from telnet/shell.');
}

PAIR: foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

($value eq "---") and next PAIR;
exists $in{$name} ? ($in{$name} .= "~~$value") : ($in{$name} = $value);
}
return %in;
}
#################################################
%in=&parse_form;
print "Content-type: text/html\n\n";
use LWP::Simple;
$content = get("url");
push(@array, $content);
foreach $line (@array)
{
@pic=split(/<TABLE BORDER=0 WIDTH=440>/, $line);
@pic2=split(/<\/TABLE>/, $pic[1]);
$table = "$pic2[0]";
}
push(@results, $table);
foreach $result(@results){
($tmp, $pic) = split(/<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>/i, $result);
($pic, $tmp) = split(/<\/TD>/i, $pic);
($ttp, $info) = split(/<TD WIDTH=100 VALIGN="top">/i, $result);
($info, $ttp) = split(/<\/TD>/i, $info);
print "$pic";
</pre><HR></BLOCKQUOTE>

And you know the html

Thanks,

perlkid

[This message has been edited by perlkid (edited 08-03-2000).]


perlkid
stranger

Aug 3, 2000, 11:32 PM

Post #6 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

 I wanted to grab all thumbnails.

Not just the first one, I want all 10. Will that still work?

perlkid

[This message has been edited by perlkid (edited 08-04-2000).]


dws
Deleted

Aug 4, 2000, 8:27 AM

Post #7 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

The code fragment above should give you all of the thumbnails in table. What happened when you tried it? (You did try it, didn't you?)


[This message has been edited by dws (edited 08-04-2000).]


dws
Deleted

Aug 4, 2000, 10:24 AM

Post #8 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

That's a masochistic use of split(). Also, it looks like you're going after the first thumbnail in the table, even though there are more than one. Is that intentional?

It's simpler to do something along the lines of
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

foreach $page (@array) {
$page =~ m|<table border=0 width=440>(.*?)</table>|;
print ($table = $1) =~ /(<img.*?> )/g;
}</pre><HR></BLOCKQUOTE>


[This message has been edited by dws (edited 08-04-2000).]


perlkid
stranger

Aug 4, 2000, 5:09 PM

Post #9 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

 
It didn't work. Frown

I used

foreach $page (@table)
{
$page =~ m|<TD WIDTH=120 VALIGN=TOP ALIGN=CENTER>(.*?)</TD>|;
print ($table2 = $1) =~ /(<img.*?> )/g;
}

Did I do anything wrong

perlkid


dws
Deleted

Aug 4, 2000, 5:34 PM

Post #10 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

Tack an 's' modifier onto the end of the first regexp, to that '.' will match \n.

It looks like you've got a space after <img.*?> before the closing paren. I posted it that way but fixed it a few minutes later. Perhaps you snagged the first version.


perlkid
stranger

Aug 4, 2000, 6:50 PM

Post #11 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

 
I put the s modifier in the m&#0124; &#0124; function and it now prints the first thumbnail. Wich is the same thing that split was doing.

Cool to know that s works for new lines also, I'll be using that in the future.

perlkid


perlkid
stranger

Aug 4, 2000, 7:27 PM

Post #12 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

 
And the space wasn't it either. Frown

perlkid


dws
Deleted

Aug 4, 2000, 8:21 PM

Post #13 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

Here's an exercise to get you further up the learning curve.

Look at the input you posted, then look at your first regular expression. If it helps, print the input and use a highlighter to mark what you think is being matched. Now, add a print statement and print $1. Was it what you thought it was? Think about why not.

Now compare your regular expression with the one in the first code snipped I posted. How is it different? Can you guess by looking at it what it will match?


perlkid
stranger

Aug 4, 2000, 9:27 PM

Post #14 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

 
Sounds like a lesson in logic.

Well i did it and here is what i came up with,

I printed $1.

I expect to get all of the html that matches the regex but I only get the first html that does. Wich is an image.

I compared it with your code. I can't figure out why either of the codes didn't get the results I expected.

I think I know what the code will do before running it because I understand all of the functions and symbols.

But when I run it I'm wrong. Then I'm lost because I don't beleive that logistics are being applied. But that's not possible. Now i have no theory why I'm not getting what the code clearly demands and I think that there is some other circumstance is interfering with my programm. It's a fairly simple program and with that now in mind I don't beleive it's likley that another circumstance could cause a problem, but only my code.

I hope when I write my spider that I won't have the same problems getting all of the links off of the page. (Similar to this program.)

Well, I'm stumped. My program isn't longer than a page view and I see nothing that could be causing a problem, or is not correct. Your code looks like it should work. And so does mine.

I Thank you For your help. I'm sure if you were here you could figure it out but your not so I'll have to find an alternative or just forget my spider.

Thanks Dws Smile

perlkid


dws
Deleted

Aug 4, 2000, 11:34 PM

Post #15 of 15 (8135 views)
Re: Split Function acting up [In reply to] Can't Post

Oops. I used lower-case text in my regexps and forgot to use an 'i' modifier to make them case-independent. My bad.

Try this
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

foreach $page (@array) {
$page =~ m|<table border=0 width=440>(.*?)</table>|is;
print join "\n", ($table = $1) =~ /(<img.*?> )/ig;
}</pre><HR></BLOCKQUOTE>
I threw in the join so that you'd get one image per line.


 
 


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

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