Perl File I/O and Arrays
Perl File I/O and Arrays
}
Reading from an open file
Once the file is open, the Filehandle uses the same
syntax as <STDIN>
open FP, "file";
$line = <FP>;
When accessing a file you need <> around the
filehandle
End of File marker
Perl makes it very easy to read until the EOF
while( $line = <FP>) {
chomp($line); # remove the EOL marker.
#process input
}
It exits the loop on the end of file.
Actually the file handle returns undef, which is a false value.
closing the file
use the close command
close (filehandle);
() are optional in both close and open statements.
complete example of reading a file
To "declare" an array
my @arr;
Array syntax
Like c/c++ syntax
$arr[0] = 1;
But you can't use $arr++;
without the [], perl assumes it is a scalar variable.
The @ vs the $.
In most cases the $ is used when accessing
arrays, so
$arr[0] = 12;
A general rule of thumb, when accessing a slice of an
array, use $
when referring to the entire array, use @
Arrays
like all perl variables, arrays are type less
$arr[0] = 1; $arr[1] = "Jim";
And they can be as many dimensions as needed.
$arr[0][0] =1; $arr[0][1] = "Jim";
Since you can't declare how big an array is, perl allows
you to uses as many slices as needed.
The cell is only allocated in memory when you use it
So $arr[0] =1; $arr[1000] =2;
Uses only two spots in memory, instead of 1000
The indices cannot be negative on assignments.
$arr[-20] = 3; # maybe syntax error. * See slide on indices
Unused spots, return "" as a result, so
print $arr[50]; #prints nothing.
the Whole array
Initializing an array
@stuff = ("one", "two", "three");
@a = $stuff[0, 2 .. 4];
copy only slice 0, 2,3,4 into @a
($v1,$v2,$v3) = $stuff[0,2,3];
copy slice 0 into $v1, slice 2 into $v2, etc.
functions for arrays (lists)
sort
defaults to sort based on string value ascending
array = sort array
@x = sort @y
reverse
returns a list of elements in the opposite order
@x = reverse @y;
foreach $i (0 .. $#stuff) {
print "$stuff[$i] \n";
}
or only the range you want
foreach $i (0, 2 .. $#stuff) {
If the array is shorter than 2, it will stop.
print "$stuff[$i] \n";
}
interpolation of arrays to a string (1)
print will printout the values of 1 dimensional
print "@arr \n";
prints values with a single space between them
It doesn't matter what precedes or follows the
array, still prints the same
print @arr, "\n";
print the array with no space between
print "@2Darray";
prints the pointers to the arrays, instead of values
interpolation of arrays to a string (2)
$str = "@arr";
produces a string of the elements, separated by a
space.
$str = "@2Darray";
same as printing, the string has the pointers.
$str = "$2Darray[0]";
produces a string with the pointer to slice 0
$str = "@{$2Darray[0]}";
produces a string of values from the that section.
To get all the values into one string
foreach $i (0 .. $#2Darray) {
$str .= "@{$2Darray[$i]} ";
}
Input and arrays
Remember that <STDIN> reads until the end of
a line (same of file I/O)
Unlike c/c++ and most languages, perl reads
the entire line into a variable.
So $x = <STDIN>; #gets the whole line
chomp($x); #get rid of the end of line marker.
Now what?