Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Perl formatting beginner

by ww (Archbishop)
on Apr 15, 2014 at 16:06 UTC ( [id://1082359]=note: print w/replies, xml ) Need Help??


in reply to Perl formatting beginner

  1. 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
  2. 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).
  3. 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:
  1. code
  2. verbatim error and/or warning messages
  3. a coherent explanation of what "doesn't work actually means.

check Ln42!

Replies are listed 'Best First'.
Re^2: Perl formatting beginner
by Tux (Canon) on Apr 15, 2014 at 18:00 UTC

    It also ignores nested comma-characters, which IIRC are quite common in addresses. It also disables addresses so complicated they need embedded newlines.

    I like the approach, but please advice the use of Text::CSV_XS and/or Text::CSV.


    Enjoy, Have FUN! H.Merijn

      ... but please advice the use of Text::CSV_XS and/or Text::CSV..

      I think ww did "advise" that on the second point, though he didn't mention one.

      ..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).

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1082359]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found