advertisement
Forums

The Forum is sponsored by 
 

AAPL stock: Click Here

You are currently viewing the Tips and Deals forum
Perl is really cool
Posted by: Greg the dogsitter
Date: May 15, 2006 10:33AM
my $FileName = '/Users/greg/Desktop/test.txt';
open (MyFile, $FileName) or die "Cannot open $FileName: $!\n";

my @FileText = <MyFile>;

foreach (@FileText) {
print $_;
}

============================

Okay, that $_ variable is neat. I was Googling to find out how to set a variable to the contents of the current line, and...it seems Perl just uses the default $_ to point to the current line of the looped array. No other work necessary.

Cool.
Options:  Reply • Quote
Re: Perl is really cool
Posted by: TheTominator
Date: May 15, 2006 10:57AM
Instead of slurping up the file into an array in memory, you can read in the file line by line with:


while (<MyFile> ) {
print $_;
}





Edited 2 time(s). Last edit at 05/15/2006 10:58AM by TheTominator.
Options:  Reply • Quote
Re: Perl is really cool
Posted by: Greg the dogsitter
Date: May 15, 2006 11:02AM
TheTominator Wrote:
-------------------------------------------------------
> while (<MyFile> ) {
> print $_;
> }

Thanks!

Interesting that "while (<MyFile>)" seems to be making an intuitive leap, as there's no formal EOF flag.
Options:  Reply • Quote
Re: Perl is really cool
Posted by: TheTominator
Date: May 15, 2006 11:02AM
You can also use your own variable instead of the default $_.

foreach $line (@FileText){
print $line;
}
Options:  Reply • Quote
Re: Perl is really cool
Posted by: Seacrest
Date: May 15, 2006 11:07AM
Perl: There's more than one way to do it.™





I am not Ryan Seacrest, and I do not approve this message.
Options:  Reply • Quote
Re: Perl is really cool
Posted by: NewtonMP2100
Date: May 15, 2006 11:08AM
remember. . .

knit 1, perl 2. . .knit 1, perl 2. . .

grinning smiley



_____________________________________

I reject your reality and substitute my own!
Options:  Reply • Quote
Re: Perl is really cool
Posted by: Greg the dogsitter
Date: May 15, 2006 11:09AM
"The three principal virtues of a programmer are Laziness, Impatience, and Hubris."
Options:  Reply • Quote
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 76
Record Number of Users: 186 on February 20, 2020
Record Number of Guests: 5122 on October 03, 2020