Perl Split Join - Exercise Results
You Said:
Here is one way to write two programs:
- One which writes an array to a file
- The other which reads the array from the file.
In the file, the array should be delimited by colons (or whatever character you like) all on one line.
This program writes the file:
#!/usr/bin/perl -w
$filename = shift || "foo.dat";
@array = ("foo", "bar", "baz", "boz");
open(FILE, ">$filename")
or die "can't open $filename: $!\n";
print "Writing to $filename\n";
print FILE join(":", @array);
print "Done.\n";
close FILE;
This programs reads the file
#!/usr/bin/perl -w
$filename = shift || "foo.dat";
open(FILE, "<$filename")
or die "can't open $filename: $!\n";
$line = <FILE>;
close FILE;
chomp $line;
@array = split(/:/, $line);
print "This is what I read: \n";
print join(', ', @array), "\n";
