Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Read quicker than a line per time from a file handle

Reply
Thread Tools

Read quicker than a line per time from a file handle

 
 
Carlo Filippini
Guest
Posts: n/a
 
      10-02-2003
Hi
I try to measure how fast an ftp download is going on the
fly. I do something like:

$|=1;
my $count =0;
open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
while (<CMD>){
print "Result: $_ \n";
if ($_ =~ /\#/) {
$count ++;

}

The problem is that the 'ftp.script' returns me the hashes, but all
toghter. So basically $count is only 1 after execution. In other words
the output that I get is:
Result: Connected to... bla bla
Result: Downloading ...
Result: ################################################## ####
Result: Download complete

What I want is:
Result: Connected to... bla bla
Result: Downloading ...
Result: #
Result: #
Result: #
Result: #
Result: #
Result: #
Result: #
....
Result: Download complete


I have tried $|=1; but it does not help. The original script does
write the hashes one by one to STDOUT. Is there any way to force
'ftp.script' to pass me the hashes one by one? I use a windows
machine.

Thanks
Carlo
 
Reply With Quote
 
 
 
 
Boyd
Guest
Posts: n/a
 
      10-02-2003
Carlo Filippini wrote:
> Hi
> I try to measure how fast an ftp download is going on the
> fly. I do something like:
>
> $|=1;
> my $count =0;
> open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
> while (<CMD>){
> print "Result: $_ \n";
> if ($_ =~ /\#/) {
> $count ++;
>
> }
>
> The problem is that the 'ftp.script' returns me the hashes, but all
> toghter. So basically $count is only 1 after execution. In other words
> the output that I get is:
> Result: Connected to... bla bla
> Result: Downloading ...
> Result: ################################################## ####
> Result: Download complete
>
> What I want is:
> Result: Connected to... bla bla
> Result: Downloading ...
> Result: #
> Result: #
> Result: #
> Result: #
> Result: #
> Result: #
> Result: #
> ....
> Result: Download complete
>
>
> I have tried $|=1; but it does not help. The original script does
> write the hashes one by one to STDOUT. Is there any way to force
> 'ftp.script' to pass me the hashes one by one? I use a windows
> machine.
>
> Thanks
> Carlo

I sent answer directly to Carlo since my IPS's news server quit posting.
Boyd


 
Reply With Quote
 
 
 
 
Kris Wempa
Guest
Posts: n/a
 
      10-02-2003
Why don't you try using the read() function and only read 1 char at a time ?
This is how CGI programs read from STDIN. I hope that helps.

Kris

"Carlo Filippini" <> wrote in message
news: om...
> Hi
> I try to measure how fast an ftp download is going on the
> fly. I do something like:
>
> $|=1;
> my $count =0;
> open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
> while (<CMD>){
> print "Result: $_ \n";
> if ($_ =~ /\#/) {
> $count ++;
>
> }
>
> The problem is that the 'ftp.script' returns me the hashes, but all
> toghter. So basically $count is only 1 after execution. In other words
> the output that I get is:
> Result: Connected to... bla bla
> Result: Downloading ...
> Result: ################################################## ####
> Result: Download complete
>
> What I want is:
> Result: Connected to... bla bla
> Result: Downloading ...
> Result: #
> Result: #
> Result: #
> Result: #
> Result: #
> Result: #
> Result: #
> ....
> Result: Download complete
>
>
> I have tried $|=1; but it does not help. The original script does
> write the hashes one by one to STDOUT. Is there any way to force
> 'ftp.script' to pass me the hashes one by one? I use a windows
> machine.
>
> Thanks
> Carlo



 
Reply With Quote
 
Boyd
Guest
Posts: n/a
 
      10-02-2003
Carlo Filippini wrote:
> Hi
> I try to measure how fast an ftp download is going on the
> fly. I do something like:
>
> $|=1;
> my $count =0;
> open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
> while (<CMD>){
> print "Result: $_ \n";
> if ($_ =~ /\#/) {
> $count ++;
>
> }


>
> Thanks
> Carlo


I suspect the ftp.script output is not putting end-of-line characters at
the end of lines, so you may have to use the perl read command instead
of <CMD>. For example, you put (reference: perldoc -f read)
read CMD,$char,1
if you want to read one character at a time into the scalar $char. And
then you can test for '#' as you were doing. So you can try (I haven't
tested this):
$|=1;
my $count =0;
my $char;
open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
print "Result: ";
while (read CMD,$char,1){
print $char;
if ($char =~ /\#/) {
$count ++;
print "\nResult:";
}
if ($char =~ /\r|\n/) {
print "\nResult: "
} # for Windows, this may give extra blank lines
}

Boyd


 
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
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
quicker way to refresh dll in GAC other than IISreset? Tarren ASP .Net 2 09-29-2005 03:22 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
A+ Certification - 11 DVDs Price $66 - $6 per more than 2 hours of training per DVD Julian A+ Certification 0 08-17-2004 04:11 PM
a write to a byte is quicker than a bit change ? Ben_ Java 12 11-26-2003 09:32 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