Notes: Perl Lab 04
Outline
- Opening files (read-only)
- Opening files (write)
- Opening files (append)
- foreach loops
- while loops
- split
-
Opening files
Files may be opened with the open
function. The
syntax is:
open FILEHANDLE, "MODE filename"
The FILEHANDLE identifier will be used to reference the file for
read or writes. It can be named anything but is generally
written in upper case. The MODE sets whether the file is opened
with read-only access ('<'), write access ('>') or
write-append ('>>'). These mode symbols are similar to
the Unix shell redirection symbols. Examples of each mode are shown
below. The filename can be the absolute or relative paths to
the file to be opened.
-
Opening Files with Read-only Access
If no mode is specified, file opens will default to read-only
access.
|
$data_dir = '/mnt/homes/CLASSES/MET4720/LAB04';
$datafile = $data_dir.'/metar.dat';
open(DF, "$datafile") || die "cannot open file: $!";
while($line = <DF>) {
chomp($line);
if($line =~ /BTV/) {
print "$line\n";
}
}
close(DF);
exit;
|
|
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_0.pl | head -3
KBTV 232354Z 31010KT 1/4SM +SN FZFG BKN003 BKN007 OVC030 M02/M02 A3016 RMK AO2 SNE11B33 SLP220 4/001 P0003 60003
KBTV 240006Z 29005G15KT 1/4SM +SN FZFG BKN001 OVC005 M01/M02 A3017 RMK AO2 P0002
KBTV 232354Z 31010KT 1/4SM +SN FZFG BKN003 BKN007 OVC030 M02/M02 A3016=
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
|
Below is an example showing the specified access
("read-only") and a method of capturing the contents
of a file into an array.
|
$data_dir = '/mnt/homes/CLASSES/MET4720/LAB04';
$datafile = $data_dir.'/metar.dat';
open(DF, "<$datafile") || die "cannot open file: $!";
chomp(@lines = <DF>);
close(DF);
foreach $line (@lines) {
if($line =~ /BTV/) {
print "$line\n";
}
}
exit;
|
|
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_0a.pl | head -3
KBTV 232354Z 31010KT 1/4SM +SN FZFG BKN003 BKN007 OVC030 M02/M02 A3016 RMK AO2 SNE11B33 SLP220 4/001 P0003 60003
KBTV 240006Z 29005G15KT 1/4SM +SN FZFG BKN001 OVC005 M01/M02 A3017 RMK AO2 P0002
KBTV 232354Z 31010KT 1/4SM +SN FZFG BKN003 BKN007 OVC030 M02/M02 A3016=
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
|
-
Opening files for writing
The ('>') mode will cause the file to be opened for write
access. This will initially truncate the file to zero lenght if
the file exists and create the file if it does not exist.
|
$class_dir = '/mnt/homes/CLASSES/MET4720';
$datafile = $class_dir.'/LAB04/metar.dat';
$writefile = $class_dir.'/tuckerm/out.dat';
open(DF, "<$datafile") || die "cannot open file: $!";
open(OUT, ">$writefile") || die "cannot open file: $!";
while($line = <DF>) {
chomp($line);
if($line =~ /BTV/) {
print OUT "$line\n";
}
}
close(DF);
close(OUT);
exit;
|
|
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_1.pl
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
|
-
Opening files for write/append
Opening a file with the append mode is the same as opening it
for writing except that if the file exists, it will not be
overwritten but the pointer will be moved to the end of the
existing file and all file writes will begin at this point.
|
$class_dir = '/mnt/homes/CLASSES/MET4720';
$datafile = $class_dir.'/LAB04/metar.dat';
$writefile = $class_dir.'/tuckerm/out.dat';
open(DF, "<$datafile") || die "cannot open file: $!";
open(OUT, ">>$writefile") || die "cannot open file: $!";
while($line = <DF>) {
chomp($line);
if($line =~ /BTV/) {
print OUT "$line\n";
}
}
close(DF);
close(OUT);
exit;
|
-
foreach loops
More to come....
-
while loops
More to come...
-
Split
split
function splits a string into a list and
returns the values as a list (think array). The syntax for
split
:
split /PATTERN/, EXPR
The PATTERN is a regular expression that split will look for to
break the string apart into individual elements. EXPR is the
string or function that will return a string value.
A couple of simple split examples:
|
$first_string = " This is a simple string with spaces";
$second_string = "55,42,34,48,86,67,71,12";
@new_array = split(/\s+/, $first_string);
print "Here is the first array\n";
$counter = 0;
foreach $item (@new_array) {
print "$counter: $item\n";
$counter ++;
}
@another_array = split(/,/, $second_string);
print "Here is the second array\n";
$counter = 0;
foreach $item (@another_array) {
print "$counter: $item\n";
$counter ++;
}
exit;
|
Below is the output from the previous script:
|
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_3.pl
Here is the first array
0:
1: This
2: is
3: a
4: simple
5: string
6: with
7: spaces
Here is the second array
0: 55
1: 42
2: 34
3: 48
4: 86
5: 67
6: 71
7: 12
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
|
An example of split used with handling data from a file:
|
$class_dir = '/mnt/homes/CLASSES/MET4720';
$datafile = $class_dir.'/LAB04/metar.dat';
open(DF, "<$datafile") || die "cannot open file: $!";
while($line = <DF>) {
chomp($line);
if($line =~ /BTV/) {
print "BTV:\n";
@pieces = split(/\s+/, $line);
$counter = 0;
foreach $item (@pieces) {
$counter++;
print "\t$counter: $item\n";
}
}
}
close(DF);
exit;
|
Here is a sampling of the output. Note that the output is truncated
by the head
command.
|
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_4.pl | head
BTV:
1: KBTV
2: 232354Z
3: 31010KT
4: 1/4SM
5: +SN
6: FZFG
7: BKN003
8: BKN007
9: OVC030
tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
|
last updated: 18 Mar 2012 13:15