On Tue, 11 Jan 2005 11:40:30 -0500, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> i am trying to write a simple program that splits text, but when I output the 
> results I end up with SCALAR(0x15d4ee8) all over the place. Is there any way 
> to stop this? also, it appears in the output, but oddly if i write the output 
> to a file, import it, and try to s/ it out, it remains there. I know its a 
> bit messy and I will clarify my variables later, but this should work 
> shouldnt it? Thanks ~BJ
> 
> code:
> 
> open(IN,"a.html");
> @in=<IN>;
> close IN;
> open(OUT,">references.txt");
> $big=join(\n,@in);
> $a='<h3>References</h3>';
> @first=split/$a/,$big;
> $second=$first[1];
> if($second=~/<h3>/)[EMAIL PROTECTED]/<h3>/,$second;}
> [EMAIL PROTECTED]/<hr>/,$second;}
> $forth=$third[0];
> $b='</p>';
> @refs=split/$b/,$forth;
> $,="\n";
> $v='&ndash';
> print @refs;
> 
> "Nequaquam Vacuum"
> 

this is the perfect argument for use strict and use warnings.  You
issue here is scoping: your calling functions on variables those
functions can't see.  remember that in perl, variable are scoped to
the smallest enclosing box.  This lets you reuse varibales, but it
also means that if you declare a variable in an if block, you can't
use it in the main block or any other block unless you return a
reference to it.  applying some visual style to you code would help
you see this...if you indent blocks, a good rule of thumb is that you
can't use a variable unless it's declaration is on a line that starts
left of where you're going to use it.

#!/usr/bin/perl

use warnings;
use strict;

> open(IN,"a.html");
> @in=<IN>;
> close IN;
> open(OUT,">references.txt");

$big=join(\n,@in);  # not sure why you're doing this.
     # if you haven't 'chomp'ed, the line breaks are still there.
$a='<h3>References</h3>';
@first=split/$a/,$big;
$second=$first[1];   # ok, this varible can be used anywhere

if ($second=~/<h3>/ ){
     @third=split/<h3>/,$second;
     # This variable only exists until the next '}'
}
else {
     @third=split/<hr>/,$second;
     # This variable only exists until the nex '}'
}

$forth=$third[0];  
     # but there are no @third
     # one @third disappeared at the end of the if block
     # and the other disappeared at the end of the else block

$b='</p>';
@refs=split/$b/,$forth;
     # but there is nothing to split, because there is nothing in $forth
     # because there was nothing in $third
$,="\n";
$v='&ndash';
print @refs;
     # but there is nothing to print, because there are no @refs
     # because there was nothing $forth to split...
     # so perl helpfully prints some references that point to where
     # they would point if there were anything there to point to.

Taking another look at perltut.  use strict would have caught a lot of
this for you, too.

HTH,

--jay

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to