Programming Wiki: Reading Information From a File and into a Variable

Programming Wiki : PerlReadFromFile

WikiHomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
Most recent edit on 2006-04-16 20:51:32 by ScottRippee

Additions:
Reading a whole file in at once
Here's a program that reads a file and prints it in sorted order
open(MYINPUTFILE, "<filename.out"); # open for input
my(@lines) = <MYINPUTFILE>; # read file into list
@lines = sort(@lines); # sort the list
my($line);
foreach $line (@lines) # loop thru list
{
print "$line"; # print in sort order
}
close(MYINPUTFILE);
Reading a file line by line
open(MYINPUTFILE, "<filename.out");
while(<MYINPUTFILE>)
{
# Good practice to store $_ value because
# subsequent operations may change it.
my($line) = $_;
# Good practice to always strip the trailing
# newline from the line.
chomp($line);
# Convert the line to upper case.
$line =~ tr/[a-z]/[A-Z]/;
# Print the line to the screen and add a newline
print "$line\n";
}
%%




Edited on 2006-04-16 20:19:30 by ScottRippee

Additions:

Reading Information From a File and into a Variable

Perl



Deletions:

Perl

Reading Information From a File and into a Variable





Edited on 2006-04-16 20:12:19 by ScottRippee

Additions:
open(FILE,"my_file_name"); my @my_array = <FILE>;
close FILE;


Deletions:
open(FILE,"my_file_name"); my @my_array = <FILE>; close FILE;



Edited on 2006-01-06 23:02:29 by ScottRippee [Create initial page]

No differences.


Oldest known version of this page was edited on 2006-01-06 23:02:14 by ScottRippee [Created initial page]
Page view:

Perl

Reading Information From a File and into a Variable



open(FILE,"my_file_name"); my @my_array = <FILE>; close FILE;
Powered by Wikka Wakka Wiki 1.1.6.0
Page was generated in 0.0289 seconds