Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Pulling out lines of text from a text file (http://www.velocityreviews.com/forums/t896909-pulling-out-lines-of-text-from-a-text-file.html)

poopdeville@gmail.com 03-09-2006 08:54 AM

Pulling out lines of text from a text file
 
Hi everybody,

I'm looking to write a Catalyst model to basically use a newline
delimited database, so that each line of the text file corresponds to a
datum. My question isn't about the module per se, but on smart
algorithms to pull a single line of text from an arbitrary text file.
I know the following would work:

#!/usr/bin/perl

use warnings;
use strict;

print "Enter a file\n";
my $file = <STDIN>;

print "Enter a number\n";
my $number = <STDIN>;

my @array;

open FILE, "$file";

while (<FILE>) { push @array; }
close FILE;

print $array[$number];
__END__

or something close to it should work. (I hope there aren't any errors
there, but if there are, I hope you get the idea of the naive
implementation I'm talking about). An anyone point the way to a faster
algorithm?

Thanks,
'cid 'ooh


Gunnar Hjalmarsson 03-09-2006 09:08 AM

Re: Pulling out lines of text from a text file
 
poopdeville@gmail.com wrote:
> ... pull a single line of text from an arbitrary text file.
> I know the following would work:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> print "Enter a file\n";
> my $file = <STDIN>;
>
> print "Enter a number\n";
> my $number = <STDIN>;
>
> my @array;
>
> open FILE, "$file";
>
> while (<FILE>) { push @array; }
> close FILE;
>
> print $array[$number];
> __END__
>
> or something close to it should work. (I hope there aren't any errors
> there, but if there are, I hope you get the idea of the naive
> implementation I'm talking about). An anyone point the way to a faster
> algorithm?


Use the FAQ answer provided in "perldoc -q middle", i.e. Tie::File.

I leave it to you to compare the speed. ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Sisyphus 03-09-2006 09:10 AM

Re: Pulling out lines of text from a text file
 

<poopdeville@gmail.com> wrote in message
news:1141894492.223365.64420@i39g2000cwa.googlegro ups.com...
> Hi everybody,
>
> I'm looking to write a Catalyst model to basically use a newline
> delimited database, so that each line of the text file corresponds to a
> datum. My question isn't about the module per se, but on smart
> algorithms to pull a single line of text from an arbitrary text file.
> I know the following would work:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> print "Enter a file\n";
> my $file = <STDIN>;
>
> print "Enter a number\n";
> my $number = <STDIN>;
>
> my @array;
>
> open FILE, "$file";
>
> while (<FILE>) { push @array; }
> close FILE;
>
> print $array[$number];
> __END__
>
> or something close to it should work. (I hope there aren't any errors
> there, but if there are, I hope you get the idea of the naive
> implementation I'm talking about). An anyone point the way to a faster
> algorithm?
>
> Thanks,
> 'cid 'ooh
>


If I understand you correctly (also untested) :

use warnings;
use strict;

print "Enter a file\n";
my $file = <STDIN>;

print "Enter a number\n";
my $number = <STDIN>;

# Always check that open succeeds
open FILE, "$file" or die "Can't open: $!";

while (<FILE>) {
if($. == $number) {print $_}
last; # no need to keep reading
}

# Always check that close succeeds
close FILE or die "Can't close: $!";

__END__

See the documentation for $. in 'perldoc perlvar'.

Cheers,
Rob



Anno Siegel 03-09-2006 09:17 AM

Re: Pulling out lines of text from a text file
 
<poopdeville@gmail.com> wrote in comp.lang.perl.misc:
> Hi everybody,
>
> I'm looking to write a Catalyst model to basically use a newline
> delimited database, so that each line of the text file corresponds to a
> datum. My question isn't about the module per se, but on smart
> algorithms to pull a single line of text from an arbitrary text file.
> I know the following would work:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> print "Enter a file\n";
> my $file = <STDIN>;
>
> print "Enter a number\n";
> my $number = <STDIN>;
>
> my @array;
>
> open FILE, "$file";
>
> while (<FILE>) { push @array; }


You don't need an explicit loop here:

@array = <FILE>;

> close FILE;
>
> print $array[$number];
> __END__
>
> or something close to it should work. (I hope there aren't any errors
> there, but if there are, I hope you get the idea of the naive
> implementation I'm talking about). An anyone point the way to a faster
> algorithm?


Look into Tie::File, it will simplify things.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Michael Greb 03-09-2006 09:19 AM

Re: Pulling out lines of text from a text file
 
On 2006-03-09, poopdeville@gmail.com <poopdeville@gmail.com> wrote:
> Hi everybody,
>
> I'm looking to write a Catalyst model to basically use a newline
> delimited database, so that each line of the text file corresponds to a
> datum. My question isn't about the module per se, but on smart
> algorithms to pull a single line of text from an arbitrary text file.
> I know the following would work:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;


> print "Enter a file\n";


# For more of a standard prompt you probably just want:
print "Enter a file: ";

> my $file = <STDIN>;


# $file will have a \n on the end use chomp to remove it:
chomp ($file);

> print "Enter a number\n";


# Ditto:
print "Enter a number: ";

> my $number = <STDIN>;


# Ditto:
chomp ($number);

> my @array;
>
> open FILE, "$file";


# You should check the return code of open rather then assuming it worked,
# you also don't need the quotes around $file since you aren't adding
# any extra text.

open FILE, $file or die "Can't open $file: $!\n";

> while (<FILE>) { push @array; }


# Replaces this with:

@array = <FILE>;

# @lines would be a better name for the array though, @array is
# redundant and generic, we already know it is an array from the @

> close FILE;


# Though less of an issue when you are just reading a file, you should
# get in the habit of checking the return code of close as well.

> print $array[$number];
> __END__
>
> or something close to it should work. (I hope there aren't any errors
> there, but if there are, I hope you get the idea of the naive
> implementation I'm talking about). An anyone point the way to a faster
> algorithm?


If you are just trying to read a whole file into an array by line, @var
= <FH> is better then a while loop and push. If all your program is
doing is printing out a specific line from a file (and especially if the
file is going to be large), it may be better to loop over the file until
you get to that line number (or EOF) and then print the line (or an
error). This way a 100k lines file isn't stored in RAM to print line
10.

--
Michael
michael@thegrebs.com
SpamStats: http://spam.thegrebs.com

Tad McClellan 03-09-2006 12:49 PM

Re: Pulling out lines of text from a text file
 
Sisyphus <sisyphus1@nomail.afraid.org> wrote:

> If I understand you correctly (also untested) :


> print "Enter a file\n";
> my $file = <STDIN>;



chomp $file; # ?


> # Always check that open succeeds
> open FILE, "$file" or die "Can't open: $!";

^^^^^^^
^^^^^^^

Never quote a lone variable.


perldoc -q vars

What's wrong with always quoting "$vars"?


> while (<FILE>) {
> if($. == $number) {print $_}
> last; # no need to keep reading
> }



A loop that must execute exactly zero or one time isn't much of a loop...


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Sisyphus 03-09-2006 10:25 PM

Re: Pulling out lines of text from a text file
 

"Tad McClellan"
..
..
>
> > while (<FILE>) {
> > if($. == $number) {print $_}
> > last; # no need to keep reading
> > }

>
>
> A loop that must execute exactly zero or one time isn't much of a loop...
>


Heh ... indeed .... better make that (still untested):

while (<FILE>) {
if($. == $number) {
print $_;
last; # no need to keep reading
}
}

Cheers,
Rob



Uri Guttman 03-09-2006 10:36 PM

Re: Pulling out lines of text from a text file
 
>>>>> "S" == Sisyphus <sisyphus1@nomail.afraid.org> writes:

S> "Tad McClellan"
S> .
S> .
>>
>> > while (<FILE>) {
>> > if($. == $number) {print $_}
>> > last; # no need to keep reading
>> > }

>>
>>
>> A loop that must execute exactly zero or one time isn't much of a loop...
>>


S> Heh ... indeed .... better make that (still untested):

S> while (<FILE>) {
S> if($. == $number) {
S> print $_;
S> last; # no need to keep reading
S> }
S> }

even better:

while (<FILE>) {
next if $. < $number ;
print $_ ;
last;
}

saves a whole block and those damned expensive {}. also it has a more
common style of indenting.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

poopdeville@gmail.com 03-09-2006 11:12 PM

Re: Pulling out lines of text from a text file
 

poopdeville@gmail.com wrote:
> Hi everybody,
>
> I'm looking to write a Catalyst model to basically use a newline
> delimited database, so that each line of the text file corresponds to a
> datum. My question isn't about the module per se, but on smart
> algorithms to pull a single line of text from an arbitrary text file.


Thanks everybody!
'cid 'ooh


Dr.Ruud 03-09-2006 11:46 PM

Re: Pulling out lines of text from a text file
 
Uri Guttman schreef:

> even better:
>
> while (<FILE>) {
> next if $. < $number ;
> print $_ ;
> last;
> }


while (<FILE>) {
$. == $number or next;
print;
last;
}

--
Affijn, Ruud

"Gewoon is een tijger."


All times are GMT. The time now is 01:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.