Parsing Simple Text Files II

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

MountPoints.pl
my @mountpoints = ();
while (<$in>) {
   next if /^#/;     # skip comments
   next if /^\s*$/;  # skip empty lines
   chomp;            # remove line terminator
   my $mountpoint = (split /\s+/)[2]; # pick 3rd field
   next unless $mountpoint =~ m{^/}; # must be absolute
   push(@mountpoints, $mountpoint);
}
$in->close;

print join("\n", sort @mountpoints), "\n";

*\s represents white space in regular expressions, i.e. space, tabs, line terminators etc.
 
*split takes a variable (or $_) and splits it, using its regular expression as delimiter, into a list.
 
*An expression (like split) that returns a list can immediately be indexed if it is put into parentheses.
 
*sort sorts a list by default alphabetically and returns a new list. The old list is left undisturbed.
 
*join joins all elements of the list (second parameter) and inserts the first parameter in between.
 

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2002 Andreas Borchert, converted to HTML on May 02, 2002