Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Concatentation

Reply
Thread Tools

Concatentation

 
 
Daniel Bergquist
Guest
Posts: n/a
 
      07-13-2004
Consider the following chunk of code:
--------------------------------------------------
open (IN, "<:raw", "test2.txt") or die "Can't open test.txt";

chomp($line = <IN>);

# Capture excerpt
$line =~ m/>([^<]+)/;

# Copy first line of excerpt
$pExcerpt = $1;

# Next line
chomp($line = <IN>);

# Untill we have reached the end of the section
until($line =~ m/<\/p>/i) {

# Capture useful text
$line =~ m/([^<]+)/;
chomp($line = <IN>);
}

# Capture the rest of the useful text
$line =~ m/([^<]+)/;

$pExcerpt = "$pExcerpt $1";

print "final: $pExcerpt\n";
-----------------------------------------------------------



The file test2.txt is as follows:
-------------------------------------------------
<p class=p1>I consider myself fortunate to stand before you today as I
make my
defense against all the accusations of the Jews. <i>Acts 26:2</i></p>


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

When run:
P:\WEBPOP\EXPERI~1>excerpt.pl
defense against all the accusations of the Jews. you today as I make
my

P:\WEBPOP\EXPERI~1>


When I change the concatenation to as follows:
$pExcerpt = "$1 $pExcerpt";
The result is:
P:\WEBPOP\EXPERI~1>excerpt.pl
final: defense against all the accusations of the Jews. I consider
myself fortunate
to stand before you today as I make my

P:\WEBPOP\EXPERI~1>

Which is how I would expect it to work. Why does it not work the first
way(which is the way I need it)?


Perl reports itself as v5.8.3 built for MSWin32-x86-multi-thread,
binary build 809 provided by ActiveState Corp.


Thanks!

Daniel Bergquist
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      07-13-2004
Daniel Bergquist <> wrote:
> Consider the following chunk of code:
> --------------------------------------------------
> open (IN, "<:raw", "test2.txt") or die "Can't open test.txt";
>
> chomp($line = <IN>);
>
> # Capture excerpt
> $line =~ m/>([^<]+)/;
>
> # Copy first line of excerpt
> $pExcerpt = $1;



You should never use the dollar-digit variables unless you have
first ensured that the match *succeeded*, since the variables
are only changed when the match succeeds.


$pExcerpt = $1 if $line =~ m/>([^<]+)/;


( I hope you are not trying to parse HTML or XML with regular expressions...)


> # Next line
> chomp($line = <IN>);
>
> # Untill we have reached the end of the section
> until($line =~ m/<\/p>/i) {



You should use a module that understands HTML for processing HTML data.


> # Capture useful text
> $line =~ m/([^<]+)/;



The above line of code is useless. You don't put the captured text anywhere.

What do you think that pattern match is doing for you?


> chomp($line = <IN>);
> }
>
> # Capture the rest of the useful text
> $line =~ m/([^<]+)/;
>
> $pExcerpt = "$pExcerpt $1";



$pExcerpt .= " $1";



> Why does it not work the first
> way(which is the way I need it)?



It won't matter if you process it properly (with an HTML module rather
than with regexes).


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Daniel Bergquist
Guest
Posts: n/a
 
      07-14-2004
> > # Capture useful text
> > $line =~ m/([^<]+)/;

>
>
> The above line of code is useless. You don't put the captured text anywhere.
>
> What do you think that pattern match is doing for you?


Heh, sorry, I had cut out a bunch of code and this is a work in
progress so some lines don't look useful at the moment.


> > chomp($line = <IN>);
> > }
> >
> > # Capture the rest of the useful text
> > $line =~ m/([^<]+)/;
> >
> > $pExcerpt = "$pExcerpt $1";

>
>
> $pExcerpt .= " $1";
>
>
>
> > Why does it not work the first
> > way(which is the way I need it)?

>
>
> It won't matter if you process it properly (with an HTML module rather
> than with regexes).


Opps, after a couple Google searches I know realize I have committed a
crime against the Perl community. Pardon me as I have been using Perl
only for a few months. Although I have found the modules HTML:arser
and HTML::TokeParser I am not entirely sure which to use and I would
like to read through a tutorial or two.


Thanks!

Daniel Bergquist
 
Reply With Quote
 
Daniel Bergquist
Guest
Posts: n/a
 
      07-14-2004
Nevermind, I think I'm figuring one of the parsers out.

Thanks!


Daniel Bergquist
 
Reply With Quote
 
Daniel Bergquist
Guest
Posts: n/a
 
      07-14-2004
Nevermind, I think I'm figuring one of the parsers out.

Thanks!


Daniel Bergquist
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string concatentation vs. interpolation: which one is more optimal? rihad Perl Misc 37 10-14-2007 02:35 PM
which is better, string concatentation or substitution? John Salerno Python 16 05-12-2006 05:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57