Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > new topic: I call length($<string>) and get number of lines - codefrag below - on MAC OS X 10.7

Reply
Thread Tools

new topic: I call length($<string>) and get number of lines - codefrag below - on MAC OS X 10.7

 
 
kquirici
Guest
Posts: n/a
 
      06-10-2012
if( ! open JOURNALTXT, $inputfilename) {
die "cannot open ".$inputfilename;
}

while (<JOURNALTXT>){
$journalin = $journalin . %_;
}

close JOURNALTXT;

$journal_txt_len = length($journalin);
print "journal length: " . $journal_txt_len;


-----------------
the last print statement returns 159 which is the number of lines; there are
actually around 8k chars.

At first I thought it was because I was using TextWrangler which uses unicode utf8, so I googled that and added all the stuff necessary for unicode on perl 5.whatever, but I still just got number of lines.

any help much appreciated.

Regards,

Ken Quirici
 
Reply With Quote
 
 
 
 
kquirici
Guest
Posts: n/a
 
      06-10-2012
On Sunday, June 10, 2012 2:17:45 PM UTC-4, Ben Morrow wrote:
> Quoth kquirici <(E-Mail Removed)>:
> > if( ! open JOURNALTXT, $inputfilename) {
> > die "cannot open ".$inputfilename;
> > }

>
> Checking the return value of open is very good; but if you do it this
> clumsily it will soon become infuriating and you will start leaving it
> out. You should also include $! in the error message so you know what
> went wrong.
>
> You should be keeping your filehandles in variables, rather than using
> global barewords. You should also be using 3-arg open.
>
> open my $JOURNALTXT, "<", $inputfilename
> or die "cannot open '$inputfilename': $!";
>
> Alternatively, you can 'use autodie', which will handle the error for
> you.
>
> > while (<JOURNALTXT>){
> > $journalin = $journalin . %_;

>
> What do you think this does? (Specifically, %_ in scalar context.)
>
> > }
> >
> > close JOURNALTXT;
> >
> > $journal_txt_len = length($journalin);
> > print "journal length: " . $journal_txt_len;
> >
> >
> > -----------------
> > the last print statement returns 159 which is the number of lines; there are
> > actually around 8k chars.

>
> Did you try printing $journalin, to see what's in it? It isn't what you
> expect.
>
> More generally, this is an extremely roundabout way of reading a file
> into a variable. Either use
>
> {
> local $/; # this sets $/ to undef
> $journalin = <$JOURNALTXT>;
> }
>
> or use File::Slurp.
>
> Ben


Thanks for your reply Ben. I got it to work. Oddly enuf I have no idea
why it works, because it looks exactly the same as before - I made some
mods that I deleted and returned it to its original form except now it works:

if( ! open JOURNALTXT, $inputfilename) {
die "cannot open ".$inputfilename;
}

while (<JOURNALTXT>){
$journalin = $journalin . $_;
}

close JOURNALTXT;
#print $journalin;

my $journalen = length($journalin);
print "\njournal length: " . $journalen;

prints the correct number of chars.

Thanks & Regards & Mystified,

Ken Quirici
 
Reply With Quote
 
 
 
 
kquirici
Guest
Posts: n/a
 
      06-10-2012
On Sunday, June 10, 2012 2:17:45 PM UTC-4, Ben Morrow wrote:
> Quoth kquirici <(E-Mail Removed)>:
> > if( ! open JOURNALTXT, $inputfilename) {
> > die "cannot open ".$inputfilename;
> > }

>
> Checking the return value of open is very good; but if you do it this
> clumsily it will soon become infuriating and you will start leaving it
> out. You should also include $! in the error message so you know what
> went wrong.
>
> You should be keeping your filehandles in variables, rather than using
> global barewords. You should also be using 3-arg open.
>
> open my $JOURNALTXT, "<", $inputfilename
> or die "cannot open '$inputfilename': $!";
>
> Alternatively, you can 'use autodie', which will handle the error for
> you.
>
> > while (<JOURNALTXT>){
> > $journalin = $journalin . %_;

>
> What do you think this does? (Specifically, %_ in scalar context.)
>
> > }
> >
> > close JOURNALTXT;
> >
> > $journal_txt_len = length($journalin);
> > print "journal length: " . $journal_txt_len;
> >
> >
> > -----------------
> > the last print statement returns 159 which is the number of lines; there are
> > actually around 8k chars.

>
> Did you try printing $journalin, to see what's in it? It isn't what you
> expect.
>
> More generally, this is an extremely roundabout way of reading a file
> into a variable. Either use
>
> {
> local $/; # this sets $/ to undef
> $journalin = <$JOURNALTXT>;
> }
>
> or use File::Slurp.
>
> Ben


Ah, I see what the problem was - I had %_ instead of $_.

Thanks & Regards,

Ken Quirici




>
> Checking the return value of open is very good; but if you do it this
> clumsily it will soon become infuriating and you will start leaving it
> out. You should also include $! in the error message so you know what
> went wrong.
>
> You should be keeping your filehandles in variables, rather than using
> global barewords. You should also be using 3-arg open.
>
> open my $JOURNALTXT, "<", $inputfilename
> or die "cannot open '$inputfilename': $!";
>
> Alternatively, you can 'use autodie', which will handle the error for
> you.
>
> > while (<JOURNALTXT>){
> > $journalin = $journalin . %_;

>
> What do you think this does? (Specifically, %_ in scalar context.)
>
> > }
> >
> > close JOURNALTXT;
> >
> > $journal_txt_len = length($journalin);
> > print "journal length: " . $journal_txt_len;
> >
> >
> > -----------------
> > the last print statement returns 159 which is the number of lines; there are
> > actually around 8k chars.

>
> Did you try printing $journalin, to see what's in it? It isn't what you
> expect.
>
> More generally, this is an extremely roundabout way of reading a file
> into a variable. Either use
>
> {
> local $/; # this sets $/ to undef
> $journalin = <$JOURNALTXT>;
> }
>
> or use File::Slurp.
>
> Ben


 
Reply With Quote
 
Marc Girod
Guest
Posts: n/a
 
      06-10-2012
On Jun 10, 8:46*pm, Ben Morrow <(E-Mail Removed)> wrote:

> > * *$journalin = $journalin . $_;


Sorry, but what is %_?
I cannot find it in perlvar, yet I don't get an error with:

perl -Mstrict -wle 'print %_'

Marc
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      06-11-2012
>>>>> "kquirici" == kquirici <(E-Mail Removed)> writes:

kquirici> $journalin = $journalin . %_;

Did you want %_ there, or $_?

print "Just another Perl hacker,"; # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      06-13-2012
On 2012-06-10, kquirici <(E-Mail Removed)> wrote:

> Ah, I see what the problem was - I had %_ instead of $_.
>
> Thanks & Regards,
>
> Ken Quirici



But don't disregard what Ben suggested, it's better, clearer
and more concise than what you have. In addition, if you
don't implement better methods when they're pointed out to
you then the next time you have a problem and ask here you're
going to get the same "use the three argument open", and
"local $/ for slurping" rather than getting to the meat of
your problem.


open my $JOURNALTXT, "<", $inputfilename
or die "cannot open '$inputfilename': $!";

my $journalin;
{
local $/; # this sets $/ to undef
$journalin = <$JOURNALTXT>;
}

print "journal length: ", length($journalin), "\n";



Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      06-15-2012
In article <(E-Mail Removed)>,
Justin C <(E-Mail Removed)> wrote:
> local $/; # this sets $/ to undef


I think that a rather more consise way to document that and to assure
any reader that it is true is

local $/ = undef;

or

undef(local $/);

(That latter appears to actually work.)

--
Tim McDaniel, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      06-15-2012
(E-Mail Removed) (Tim McDaniel) writes:
> In article <(E-Mail Removed)>,
> Justin C <(E-Mail Removed)> wrote:
>> local $/; # this sets $/ to undef

>
> I think that a rather more consise way to document that and to assure
> any reader that it is true is
>
> local $/ = undef;


Code is not documentation. The comment informs a reader that $/ is
actually supposed to be undef. The assignment is just useless.

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      06-17-2012
>>>>> "k" == kquirici <(E-Mail Removed)> writes:

k> if( ! open JOURNALTXT, $inputfilename) {
k> die "cannot open ".$inputfilename;
k> }

why didn't you listen to what ben said about that code? also you can
interpolated $inputfilename into the die string which is a much better
style.


k> while (<JOURNALTXT>){
k> $journalin = $journalin . $_;
k> }

that is one of the slowest and clumsiest ways to read a whole file
in. first off learn about the .= assignment op (see perldoc perlop). but
even then why are you just reading each line to append it to the buffer?
ben showed you a much better way to read a whole file and File::Slurp is
the perl standard way these days.

k> close JOURNALTXT;
k> #print $journalin;

k> my $journalen = length($journalin);
k> print "\njournal length: " . $journalen;

k> prints the correct number of chars.

if all you wanted was the size of the file, -s will do that in one
call.

uri
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      06-17-2012
In article <(E-Mail Removed)>,
Ben Morrow <(E-Mail Removed)> wrote:
>Quoth (E-Mail Removed):
>> undef(local $/);
>>
>> (That latter appears to actually work.)

>
>Is that surprising? local returns an lvalue, just like my; this is
>very similar in form to the standard
>
> (my $new = $old) =~ s/.../.../;


It's not the returning an lvalue part that I find hard to grasp --
though I had not realized that, so thank you.

It just still seems fundamentally weird to me that you can bury a
variable declaration within an expression, and further that it doesn't
end with the enclosing parentheses or expression. Yes, yes, the
perlfunc docco for "my" has "A 'my' declares the listed variables to
be local (lexically) to the enclosing block, file, or 'eval'." and
similarly for "local". And nobody else is responsible for it feeling
weird to me. Still. Weird.

--
Tim McDaniel, (E-Mail Removed)
 
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
Browser crashes with below code.What is wrong in below code. kiran Javascript 12 12-07-2011 02:38 PM
Hard or Easy? To find string, then grab criterion matched lines above and below? samiam@mytrashmail.com Perl Misc 13 10-09-2006 07:56 PM
Could someone scan me a picture of the below? (Read Below) starlightvoyager@yahoo.com DVD Video 1 08-28-2006 05:42 AM
grepping lines above and below a pattern found mike Perl Misc 15 05-21-2004 08:46 AM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments