- Perl is indeed wonderfully capable... but do you really need to do it the hard way? After all, a standard Perl remark is 'don't reinvent the wheel:' http://office.microsoft.com/en-us/templates/avery-5160-mailing-labels-featuring-avery-app-for-word-2013-anniversary-design-30-per-page-TC103749911.aspx
- But if you're still going to do it in Perl, it looks as though you have a comma-delimited source file, AKA, CSV. So use one of the csv modules to drag in your data (not that what you were doing is terribly wrong).
- And if you're still going to do it your way -- minus the formatting:
#!/usr/bin/perl
use strict;
use warning; #mandatory inclusions until you know enough to know
+when you can skip these helpers
use 5.016;
# Program name: perlReadAndFormat.pl
# Purpose: Open disk file. Read and display the records in
# the file. Count the number of records in the file.
# open (FILEIN, "cust.txt") || warn "Could not open source file\n";
open (LABEL, ">","labels-to-print.txt") || warn "Can't create labels f
+ile\n";
# while (<FILEIN>)
while (<DATA>){
#print "$_";
($CUSTID,$fname,$lname,$phone,$address,$city,$state,$zip,$email)
+= split(/,/,$_);
# Or use array: @fields = split(/,/,$_);
# write(LABEL); # send to output
print LABEL "$CUSTID,$fname,$lname\n$address,$city,$state,$zip\n$
+phone,$email\n";
++$line_count;
}
print ("File \"passwd\" has $line_count lines. \n");
close (FILEIN);
close (LABEL);
__DATA__
$CUSTID,$fname,$lname,$phone,$address,$city,$state,$zip,$email
1,fred,jones,555-1212,123 Main St.,Boston, MA,00001,test@test.com
2,mary,smith,222-1515,321 Least St.,Hanover,NH,02022,testmary@test.com
3,jack,least,122-5511,231 Last St.,Franklin,CT,06001,nomail@test.com
Output:
1,fred,jones
123 Main St.,Boston, MA,00001
555-1212,test@test.com
2,mary,smith
321 Least St.,Hanover,NH,02022
222-1515,testmary@test.com
3,jack,least
231 Last St.,Franklin,CT,06001
122-5511,nomail@test.com
Obviously, this ignores your desire to spread them over the page; that's left as an exercise...
BUT IT WOULD HAVE BEEN NICE IF YOU'D SHOWN US THE DESIRED OUTPUT!
Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
- code
- verbatim error and/or warning messages
- a coherent explanation of what "doesn't work actually means.
check Ln42!