projection
Programmer
Hi,
Very rusty at Perl. Trying to get back into it by building simple scripts. I can't figure out what is wrong with my script. Your assistance is very much appreciated!
Trying to develop a simple script to parse a file and output to a new file.
Here is the input file contents (addresses.txt):
Name: santa claus
Email: kris.kringle@np.com
Phone: 4545454545
---------
Name: easter bunny
Email: eb@basket.com
Phone: 4545454545
---------
Here is the script:
Output:
As you can see, there is a newline character after each entry. I use chomp, which is supposed to strip the newline. I tested to see if $record contained \n before and after appending. It doesn't. When I print to STDOUT, no newlines. Only when I print to a file do the newlines appear. Please save me! I have no idea what I am missing.
Thank you in advance for your help.
- Robert
Very rusty at Perl. Trying to get back into it by building simple scripts. I can't figure out what is wrong with my script. Your assistance is very much appreciated!
Trying to develop a simple script to parse a file and output to a new file.
Here is the input file contents (addresses.txt):
Name: santa claus
Email: kris.kringle@np.com
Phone: 4545454545
---------
Name: easter bunny
Email: eb@basket.com
Phone: 4545454545
---------
Here is the script:
Perl:
#!/usr/bin/perl
open (FILE, "addresses.txt");
open (FOUT, ">addr_list.txt");
while (<FILE>) {
chomp;
$line = $_;
if($line =~ m/----/) {
print FOUT "$record\n";
$record = "";
} else {
$line = substr($line, index($line," ")+1);
$record .= ($record eq "") ? $line : "\t$line";
}
}
close(FILE); close(OUT);
exit;
Output:
Code:
santa claus
kris.kringle@np.com
4545454545
easter bunny
eb@basket.com
4545454545
Thank you in advance for your help.
- Robert