> An Englishman asked an Irishman for directions to a place some
> distance away.  The Irishman replied, "T'be sure, if oi was going
> there, oi wouldn't start from here!".
> 
> This *is* a bit off-topic, but the guy needs help.

Ged, this is a wonderful thing that you do.

But, if you help someone with off-topic questions, please reply in person,
not to the list (like others did). Also make sure you stress in your
answer that this question shold be asked somewhere else. 

Why's that? Because we have worked hard to avoid a situation where the
list becomes ask-everything-and-you-will-be-answered and loose its value,
and worse its best contributors. 

Please try to keep it clean and not encourage off-topic questions. 

Thank you for understanding!

> Press `D' if you're bored already.
> 
> On Sun, 16 Jan 2000, Kader Ben wrote:
> 
> > I want to check if @rec contains the string "Unknown" but when I do
> > so the program is very very slow (this process 6M file into @rec
> > array). Is there any other away to rewrite this code?
> 
> > for ($i = 0; $i < scalar(@rec); $i++) { $rec[$i] = '"'.$rec[$i].'"'; }
> > if($rec[16] eq '"Unknown"') { Alert_Unknown_ChannelID($rec[0]); }
> > else { my $out = join(',', @rec) . "\n"; print (G $out); }
> > }
> 
> I'd really need more to go on than you've given, so I'll make some
> wild assumptions, and here goes...
> 
> It's horribly inefficient to read a big file into an array with a
> large number of elements only to process it with things like:
> 
> $rec[$i] = '"'.$rec[$i].'"';
> 
> Think about what you're asking.  Each element has to grow by a couple
> of bytes...
> 
> Maybe you can manipulate smaller chunks of the file?  If you must add
> the quotes, do it before the pieces go into the array.  If you don't
> need to do any more processing on the array, just put the first 16
> elements into it (I assume they're relatively small), something like
> the code below.  Process the 16 element array as you do now, but deal
> with the remaining input on the fly, without putting it in an array.
> Try to use $_ wherever you can.
> 
> I take it you *are* using the `-w' switch and `use strict;'?
> 
> 73,
> Ged.
> 
> #!/usr/bin/perl -w
> # Read a file, put quotes around all the lines.
> # You can probably tell I'm really a `C' programmer.
> 
> use strict;
> 
> my @rec=();            # Small array, big file
> my $fileName = "/home/ged/website/input/create/data/input/catalogue.srt";
> 
> open(FD,$fileName);
> 
> # Read first bit of file
> my $i=0;
> while( $rec[$i++]=<FD> ) { last if $i==16; }
> 
> # My file has newlines so chop 'em off before wrapping with quotes.
> # More efficient to print this inside the body of the while() above,
> # maybe you don't need the array at all...
> for( $i=0; $i<=$#rec; ) { chop($rec[$i]); print "\"$rec[$i++]\"\n"; }
> 
> # Announce
> print "***** Here we are at line 16. *****\n";
> 
> # Add quotes on the fly.  O'course we don't have to do it at all...
> if( 1 ) { while( <FD> ) { chop; print "\"$_\"\n"; } }
> 
> close(FD);
> 
> # EOF: ged.pl
> 
> 
> 



_______________________________________________________________________
Stas Bekman    mailto:[EMAIL PROTECTED]      http://www.stason.org/stas
Perl,CGI,Apache,Linux,Web,Java,PC     http://www.stason.org/stas/TULARC
perl.apache.org    modperl.sourcegarden.org   perlmonth.com    perl.org
single o-> + single o-+ = singlesheaven    http://www.singlesheaven.com

Reply via email to