I'm confused 
Author Message
 I'm confused

Hi there,

I'm new to perl and I am writing a simple script now.
It is something like this

open (IF, "somefile");
array = &functionA(IF);

now functionA is something like
local *IFF = shift;

while (<IFF>){
   print "Line num= $i++";
   push temp, $_;

Quote:
}

return temp;

OK the program runs exactly like I wanted, it prints the line number
simultaneously while the functionA reads the file (line by line)

Then I added more lines of code to the functionA and now when I run the
program it just wait until it reads the whole file (until EOF) then it
prints the line numbers as if it's not inside the while loop.

I need to print the line simultaneously while reading the file since it
will tell me the progression of the reading (ie, Reading line 80)

Any advice would be appreciated.

ps: Second question, can we find the total number of lines in a file
from the file handle? (of course without reading the whole file first)
I want to produce something like Reading line 80/1000

Thank you.
U.D

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                \=/,         _-===-_-=======-_-===-_-=============-_-====-_

      /\  /\   / (___,,,}_--=     ___             ___            _          )
     ) /^\) ^\/ _)        =__    | . \ _ _  ___  / __> ___  ___ | |_  __    )
     )   /^\/   _)          (_   |   /| | |/ . \ \__ \<_> |/ ._>| . \<_> |   )
     )   _ /  / _)            (  |_\_\`_. |\___/ <___/<___|\___.|___/<___|  )
 /\  )/\/ ||  | )_)            (_     <___'                                 )
<  >      |(,,) )__)             (                                         )
-||------/    \)___)\-------------(_                                       __)
 | \____(      )___) )_v_           -====-_____-========-_____-======-___==  
  \______(_______;;; __;;;      v  v                          x  v   v
      __==                          v       _==---===VV===  v   v        v
____________________________________________________________________________



Tue, 11 Jan 2000 03:00:00 GMT  
 I'm confused

Quote:

>Hi there,
>I'm new to perl and I am writing a simple script now.
>It is something like this
>open (IF, "somefile");
>array = &functionA(IF);
>now functionA is something like
>local *IFF = shift;

>while (<IFF>){
>   print "Line num= $i++";
>   push temp, $_;
>}
>return temp;
>OK the program runs exactly like I wanted, it prints the line number
>simultaneously while the functionA reads the file (line by line)
>Then I added more lines of code to the functionA and now when I run the
>program it just wait until it reads the whole file (until EOF) then it
>prints the line numbers as if it's not inside the while loop.

What did you add?

Quote:
>ps: Second question, can we find the total number of lines in a file
>from the file handle? (of course without reading the whole file first)
>I want to produce something like Reading line 80/1000

Try doing a UNIX command like:
         wc -l

This will return the number of lines to stdout. Read that and you are
set.



Tue, 11 Jan 2000 03:00:00 GMT  
 I'm confused

Quote:

> Subject: I'm confused

Please check out this helpful information on choosing good subject
lines. It will be a big help to you in making it more likely that your
requests will be answered.

    http://www.perl.com/CPAN/authors/Dean_Roehrich/subjects.post

Quote:
> open (IF, "somefile");

You should always check the return value from open, even (or especially!)
in example scripts.

Quote:
> array = &functionA(IF);

It is better to write Perl, instead of making up a language as you go
along. Maybe you mean this?


You should probably see about passing a filehandle via a reference.

Quote:
> now functionA is something like
> local *IFF = shift;



variable.

Quote:
> while (<IFF>){
>    print "Line num= $i++";

Once again, that's not doing what it looks like. Inside quotes, "++" is
just a couple of punctuation marks.

Quote:
>    push temp, $_;
> }
> return temp;

> OK the program runs exactly like I wanted, it prints the line number
> simultaneously while the functionA reads the file (line by line)

> Then I added more lines of code to the functionA and now when I run the
> program it just wait until it reads the whole file (until EOF) then it
> prints the line numbers as if it's not inside the while loop.

Sounds like a buffering problem. Check out $| in perlvar(1).

Quote:
> ps: Second question, can we find the total number of lines in a file
> from the file handle? (of course without reading the whole file first)

No. There's no ESP module which can tell what's in the file without
reading it. :-)

Hope this helps!

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



Tue, 11 Jan 2000 03:00:00 GMT  
 I'm confused

Quote:

> I'm new to perl and I am writing a simple script now.
> It is something like this

> open (IF, "somefile");

Warning warning warning.  Test open statements:

open( IF, "somefile") or die "cannot open the file ($!)";

Quote:
>  array = &functionA(IF);

> now functionA is something like
> local *IFF = shift;

local or my?  

Quote:

> while (<IFF>){
>    print "Line num= $i++";
>    push temp, $_;
> }
> return temp;

> OK the program runs exactly like I wanted, it prints the line number
> simultaneously while the functionA reads the file (line by line)

> Then I added more lines of code to the functionA and now when I run the
> program it just wait until it reads the whole file (until EOF) then it
> prints the line numbers as if it's not inside the while loop.

> I need to print the line simultaneously while reading the file since it
> will tell me the progression of the reading (ie, Reading line 80)

> Any advice would be appreciated.

Because printing to the screen is such an intensive task (well, not really,
but compared to other things it is), it gets "buffered" and when the buffer
gets filled, it dumps it in one fell swoop.  
Do this:  
        $|++;
But, first, look it up to understand it.

Quote:
> ps: Second question, can we find the total number of lines in a file
> from the file handle? (of course without reading the whole file first)
> I want to produce something like Reading line 80/1000

There is not a perl way to do this (that I know of).  Try:

        $numlines = `wc -l filename`;

--
Clark Dorman                            "Evolution is cleverer than you are."
http://cns-web.bu.edu/pub/dorman/D.html                -Francis Crick



Tue, 11 Jan 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. I'm confused: @x::ISA

2. Message Q's - confused!

3. I'm confused

4. Probably a very simple question, but I'm confused..Perl on NT

5. Okay, NOW I'm confused (redirect)

6. I'm confused, help please...

7. Perl for Windows - I'm confused...

8. How many patches are there to 5.001? I'm confused.

9. Learning Perl (chap 2 q 4); I'm confused

10. Parser gets confused with '@' in strings

11. still confused about 'my'

12. grep example's use of $_ confusing me.

 

 
Powered by phpBB® Forum Software