Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: perl head a...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 10999 of 11531
Post > Topic >>

Re: perl head and tail command ?

by rob.dixon@[EMAIL PROTECTED] (Rob Dixon) Apr 28, 2008 at 07:12 PM

Richard Lee wrote:
> Rob Dixon wrote:
>> Richard Lee wrote:
>>   
>>> I would imagine linux's head command can be replaced w/ chop
>>> I asked this because I have a filehandle which was,
>>>
>>> open $source, '/tmp/server.txt'   ( and no doing head -1
/tmp/server.txt 
>>> is not an option since I need to go through some other stuff
>>> before i need to issue below command )
>>>
>>> and I wanted to do
>>>
>>> my $top = `head -1 $source`
>>> my $bottom = `tail -1 $source`
>>>
>>> but I realized I cannot do $source in back tick.
>>>
>>> so I imagine i can do
>>>
>>> my $top = chop $source;
>>>
>>> But what about the $bottom one?
>>>     
>>
>> The chop() function simply removes the last character from a string and
returns it. Since $source isn't a string you would get an error. Your best
bet is likely to be the Tie::File module. For instance
>>
>>   use strict;
>>   use warnings;
>>
>>   use Tie::File;
>>
>>   tie my @[EMAIL PROTECTED]
 'Tie::File', '/tmp/server.txt' or die $!;
>>
>>   my ($top, $bottom) = @[EMAIL PROTECTED]
 -1];
>>
>> HTH,
>>
>> Rob
>>   
> 
> 
> when you are doing it to filehandle vs array, which consumes more
memory?
> 
> my source file is about 100,000 lines...
> 
> open my $source, 'tmp/server.txt' or die !$;
> 
>                      VS
> tie my @[EMAIL PROTECTED]
 'Tie::File', '/tmp/server.txt' or die $!;

Neither of them use any significant memory until you start reading from
the file. Unless you mess about with file positioning with seek() and
tell(). the only way to retrieve the last record in a file using a
standard file handle is to read all the way through it one line at a time.
The do***entation for Tie::File says this:

>     The file is *not* loaded into memory, so this will work even for
>     gigantic files.

So it is likely to be best for your needs.

However, you should not worry about speed or memory usage until you have
written the clearest program you can and then find that it consumes too
many resources.

HTH,

Rob
 




 5 Posts in Topic:
perl head and tail command ?
rich.japh@[EMAIL PROTECTE  2008-04-28 13:31:22 
Re: perl head and tail command ?
rob.dixon@[EMAIL PROTECTE  2008-04-28 18:56:06 
Re: perl head and tail command ?
rob.dixon@[EMAIL PROTECTE  2008-04-28 19:12:39 
Re: perl head and tail command ?
noreply@[EMAIL PROTECTED]  2008-04-28 20:40:04 
Re: perl head and tail command ?
rich.japh@[EMAIL PROTECTE  2008-04-28 14:04:17 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 19:08:20 CDT 2008.