CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Post deleted by darkwguck452

 



darkwguck452
Novice

Apr 15, 2014, 4:56 PM

Post #1 of 14 (3512 views)
Post deleted by darkwguck452

 


FishMonger
Veteran / Moderator

Apr 15, 2014, 8:49 PM

Post #2 of 14 (3503 views)
Re: [rduckett14] perl formatting [In reply to] Can't Post

Did you try the suggestions given to you earlier today on your perlmonks thread?
http://www.perlmonks.org/?node_id=1082353


darkwguck452
Novice

Apr 15, 2014, 9:10 PM

Post #3 of 14 (3499 views)
Re: [FishMonger] perl formatting [In reply to] Can't Post

I did. I don't want it seem like I'm a forum hopping looking help but I did I ran what they suggested to see what that was like and then edited mine and I was able to get output and I am able to read all the lines in the input file but now I'm trying figure out how to do formatting to make it look the way that I want but I don't know where to start.


BillKSmith
Veteran

Apr 15, 2014, 9:18 PM

Post #4 of 14 (3497 views)
Re: [rduckett14] perl formatting [In reply to] Can't Post

This is a fine place for your post. So is perlmonks, but it would be polite to reference your other post(s) lest someone waste time answering a question that has already been answered elsewhere.

This is one of the rare times that I have to say that another tool is better suite to your task than perl. Use Word!

Perhaps you do have a valid reason to prefer perl. Please explain. Tell us what OS you are using and tell us about your printer.

I see that you are creating a text file rather than printing. How do you plan to print this file? This can be a challenge in Windows.

You must work with three labels at once. Print the first line of each, then the second line of each, etc. I think it would be easier to do the formatting with either printf or pack rather than FORMAT.
Good Luck,
Bill


darkwguck452
Novice

Apr 15, 2014, 9:30 PM

Post #5 of 14 (3493 views)
Re: [BillKSmith] perl formatting [In reply to] Can't Post

I am using ubuntu. As for the reason it is an assignment that I am working on where wasn't much instruction so I am learning as i am write the script thats why perl. The script just outputs a formatted txt file I wont be printing to anything it will just be creating the file


BillKSmith
Veteran

Apr 16, 2014, 6:20 AM

Post #6 of 14 (3487 views)
Re: [rduckett14] perl formatting [In reply to] Can't Post

It sounds like your task is merely to arrange the entries in three columns, without the messy details of actually centering them on labels.

My previous advice still holds. You must work with three addresses at a time. FORMAT should work fine for this, but I would still prefer printf in part because the required scope of the arguments is much smaller. Note a FORMAT must format three addresses at once.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Apr 16, 2014, 6:38 AM

Post #7 of 14 (3487 views)
Re: [rduckett14] perl formatting [In reply to] Can't Post

Have you tried using Open Office? It has a template for those labels.

http://templates.openoffice.org/en/template/blank-address-label-template
http://www.worldlabel.com/Pages/openoffice-template.htm

If you don't want to go that route, then you can load your data into an appropriate data structure (possibly an AoH) and work with 3 records at a time using printf to output each row of data for those records.

As has already been mentioned by others (on perlmonks and here) perl is not the best choice for this task, but it can be done.


FishMonger
Veteran / Moderator

Apr 16, 2014, 7:37 AM

Post #8 of 14 (3481 views)
Re: [rduckett14] perl formatting [In reply to] Can't Post

The avery 5160 layout is designed to have 4 rows per label. Your format is using 8 lines, which doesn't fit the format that you say you want to use.

See if this script does what you need which uses a 6 line format.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Text::CSV_XS;

my %customers;
my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\n" });

open my $fh, '<', 'cust.txt' or die "Can't open 'cust.txt' <$!>";

my $header = $csv->getline($fh);
shift @$header;

while (my $customer = $csv->getline($fh)) {
my $id = shift @$customer;
my %info;
@info{@$header} = @$customer;
$customers{$id} = \%info;
}
my @cust_id = sort keys %customers;

while (@cust_id) {
my @group = splice(@cust_id, 0, 3);

foreach my $id (@group) {
printf("%-30d", $id);
}
print "\n";

foreach my $id (@group) {
printf("%-30s", "$customers{$id}{'First Name'} $customers{$id}{'Last Name'}");
}
print "\n";

foreach my $id (@group) {
printf("%-30s", $customers{$id}{'Phone'});
}
print "\n";

foreach my $id (@group) {
printf("%-30s", $customers{$id}{'Address'});
}
print "\n";

foreach my $id (@group) {
printf("%-30s", "$customers{$id}{'City'}, $customers{$id}{'State'} $customers{$id}{'Zip'}");
}
print "\n";

foreach my $id (@group) {
printf("%-30s", $customers{$id}{'E-mail Address'});
}
print "\n\n";
}



(This post was edited by FishMonger on Apr 16, 2014, 7:48 AM)


darkwguck452
Novice

Apr 16, 2014, 4:21 PM

Post #9 of 14 (3471 views)
Re: [FishMonger] perl formatting [In reply to] Can't Post

This is exactly what I was lookingCool for but how would you output this data instead of it going to the terminal to a file. Would you put print OUTFILE on each print statement


(This post was edited by rduckett14 on Apr 16, 2014, 5:18 PM)


FishMonger
Veteran / Moderator

Apr 16, 2014, 6:34 PM

Post #10 of 14 (3441 views)
Re: [rduckett14] perl formatting [In reply to] Can't Post

Yes, but I'd use a lexical filehandle instead of a bareword filehandle as you suggest.


darkwguck452
Novice

Apr 16, 2014, 6:39 PM

Post #11 of 14 (3436 views)
Re: [FishMonger] perl formatting [In reply to] Can't Post

so would be like this

Code
my $out = "results2.txt"; 

my %customers;
my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\n" });

open my $fh, '<', 'cust.txt' or die "Can't open 'cust.txt' <$!>";
open my $outFile, '>$out') or die $!;


and then on the print statements like this

Code
  foreach my $id (@group) {  
printf("%-30s", $customers{$id}{'Phone'});
}
print $out "\n";



FishMonger
Veteran / Moderator

Apr 16, 2014, 6:57 PM

Post #12 of 14 (3429 views)
Re: [rduckett14] perl formatting [In reply to] Can't Post


Code
open my $outFile, '>$out') or die $!;

Should be:

Code
open my $outFile, '>', $out or die "Can't open '$out' $!";



Code
printf("%-30s", $customers{$id}{'Phone'});

Should be:

Code
printf $outFile "%-30s", $customers{$id}{'Phone'};



(This post was edited by FishMonger on Apr 16, 2014, 6:58 PM)


darkwguck452
Novice

Apr 16, 2014, 7:11 PM

Post #13 of 14 (3423 views)
Re: [FishMonger] perl formatting [In reply to] Can't Post

thanks that worked by any chance would know of any sites that have information about "%-30s" and "%-30d" because nothing is lining up in the txt file it is jumbled up. I started messing with these numbers its changing the format but I dont know how to correctly change them to work to make everything line back up


FishMonger
Veteran / Moderator

Apr 16, 2014, 8:26 PM

Post #14 of 14 (3403 views)
Re: [rduckett14] perl formatting [In reply to] Can't Post

http://perldoc.perl.org/functions/printf.html
http://perldoc.perl.org/functions/sprintf.html

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives