Ask a Question related to PERL Beginners, Design and Development.
-
Steve Hemond #1
Align text
Hi again,
Thanks for you help with my data structure problem, a hash of hashes
problem did the job :-)
I would like to know how to align text with the print command.
I have four scalar variables to print but I want them to follow their
header's size.
So, if the header is 8 chars long, I would like the output of $user
(which is paul (4 chars) to print 4 more spaces)
How could I do that?
Thanks a lot!
Best regards,
Steve Hemond
Programmeur Analyste / Analyst Programmer
Smurfit-Stone, Ressources Forestieres
La Tuque, P.Q.
Tel.: (819) 676-8100 X2833
[email]shemond@smurfit.com[/email]
Steve Hemond Guest
-
Text Align is Grayed Out
I am having an issue with one particular site. The site was created in DW CS3 using the built in style sheet templates. When I try to edit the site... -
text fields: no right align ???
Hi, I can't seem to make text align to the right (or to the center) in a text field. Am I blind or is it just not possible ? In any case, such an... -
How could i align the text in a column?
All the columns in my I'd like to align the columns to the right, left or center but i don't find a property to do it. Is it possible? Thanks -
how to Align text left & vertical align middle
Hello, I have a asp lable control, which I use to display text in, I would like to have the text display aligned in the center and vertical... -
Text Align
When I install my accesss 2000 application on some machines the application ignores the "Text Align" Property Setting in some Activex Controls(List... -
James Edward Gray II #2
Re: Align text
\On Dec 16, 2003, at 1:39 PM, Hemond, Steve wrote:
You're looking for printf(). See if this documentation fixes you up:> Hi again,
>
> Thanks for you help with my data structure problem, a hash of hashes
> problem did the job :-)
>
> I would like to know how to align text with the print command.
>
> I have four scalar variables to print but I want them to follow their
> header's size.
>
> So, if the header is 8 chars long, I would like the output of $user
> (which is paul (4 chars) to print 4 more spaces)
>
> How could I do that?
perldoc -f printf
perldoc -f sprintf
James
James Edward Gray II Guest
-
James Edward Gray II #3
Re: Align Text
On Dec 18, 2003, at 12:03 AM, Bill Jastram wrote:
I'm happy to help, but keep your replies on the list so we can all> James:
learn/help.
You bet:> Can printf set the value in spaces of tab (\t)? I need to do three
> column
> output with perl for mailing labels.
James Edward Gray>perl -e 'printf "%20s %20s %20s\n", "James", "Edward", "Gray"'
Three columns of 20 character width right there. Does that answer your
question?
James
James Edward Gray II Guest
-
James Edward Gray II #4
Re: Align Text
On Dec 19, 2003, at 8:29 AM, Bill Jastram wrote:
The list is just another address you can send your messages too:> James:
>
> A coupe of things.
>
> #1. Pardon my ignorance, but I'm not sure how to use the list where I
> found the reply button to e-mail you directly. I could not find a way
> to
> add to the already existing 'thread'. Your help would be appreciated.
[email]beginners@perl.org[/email]
A lot of people just hit "Reply All" if their mail client has such a
button. In the case of this message, that would send your answer to me
and the list.
Correct, but they will be aligned flush right. For flush left, we just> #2. The challenge with the use of %20s is that in the next row of
> records
> the names will not be aligned (flush left) with the row above, if the
> names are not the same length. This is my dilemma.
change the pattern a little:
James Edward Gray> perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
Does that answer your question?
James
James Edward Gray II Guest
-
Tom Kinzer #5
RE: Align Text
yes, and if that won't work for you, check out the 'format' command, you can
do lots of slick stuff with it, like centering.
-Tom Kinzer
-----Original Message-----
From: James Edward Gray II [mailto:james@grayproductions.net]
Sent: Friday, December 19, 2003 9:16 AM
To: Bill Jastram
Cc: Perl List
Subject: Re: Align Text
On Dec 19, 2003, at 8:29 AM, Bill Jastram wrote:
The list is just another address you can send your messages too:> James:
>
> A coupe of things.
>
> #1. Pardon my ignorance, but I'm not sure how to use the list where I
> found the reply button to e-mail you directly. I could not find a way
> to
> add to the already existing 'thread'. Your help would be appreciated.
[email]beginners@perl.org[/email]
A lot of people just hit "Reply All" if their mail client has such a
button. In the case of this message, that would send your answer to me
and the list.
Correct, but they will be aligned flush right. For flush left, we just> #2. The challenge with the use of %20s is that in the next row of
> records
> the names will not be aligned (flush left) with the row above, if the
> names are not the same length. This is my dilemma.
change the pattern a little:
James Edward Gray> perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
Does that answer your question?
James
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Tom Kinzer Guest
-
Bill Jastram #6
RE: Align Text
We're getting closer. But lets say the first name of the first field in the first row is 'Bill'. And the first name of the first field in the second row is 'Lanette'. This command will not compensate for the difference in the length of the two first names. So, the alignment of the second fields in each row will not be correct. And so on ...
Does this help you understand where I'm headed?
Bill
__________________________________________________
>yes, and if that won't work for you, check out the 'format' command,
>you can
>do lots of slick stuff with it, like centering.
>
>-Tom Kinzer
>
>-----Original Message-----
>From: James Edward Gray II [mailto:james@grayproductions.net]
>Sent: Friday, December 19, 2003 9:16 AM
>To: Bill Jastram
>Cc: Perl List
>Subject: Re: Align Text
>
>
>On Dec 19, 2003, at 8:29 AM, Bill Jastram wrote:
>>where I>> James:
>>
>> A coupe of things.
>>
>> #1. Pardon my ignorance, but I'm not sure how to use the list>way>> found the reply button to e-mail you directly. I could not find a>appreciated.>> to
>> add to the already existing 'thread'. Your help would be
>
>The list is just another address you can send your messages too:
>
>beginners@perl.org
>
>A lot of people just hit "Reply All" if their mail client has such a
>button. In the case of this message, that would send your answer to
>me
>and the list.
>>>> #2. The challenge with the use of %20s is that in the next row of
>> records
>> the names will not be aligned (flush left) with the row above, if the
>> names are not the same length. This is my dilemma.
>Correct, but they will be aligned flush right. For flush left, we just
>change the pattern a little:
>>James Edward Gray> > perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
>
>Does that answer your question?
>
>James
>
>
>--
>To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
>For additional commands, e-mail: [email]beginners-help@perl.org[/email]
><http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>Bill Jastram Guest
-
Jeff Westman #7
RE: Align Text
Bill Jastram <tfc4@cybcon.com> wrote:
Can you give an example of what you want your output to look like?
From what I am hearing you say, you probably should be using 'format', as one
already responded.
-Jeff
> We're getting closer. But lets say the first name of the first field in the
> first row is 'Bill'. And the first name of the first field in the second row
> is 'Lanette'. This command will not compensate for the difference in the
> length of the two first names. So, the alignment of the second fields in
> each row will not be correct. And so on ...
>
> Does this help you understand where I'm headed?
>
> Bill
> __________________________________________________
>>> >yes, and if that won't work for you, check out the 'format' command,
> >you can
> >do lots of slick stuff with it, like centering.
> >
> >-Tom Kinzer
> >
> >-----Original Message-----
> >From: James Edward Gray II [mailto:james@grayproductions.net]
> >Sent: Friday, December 19, 2003 9:16 AM
> >To: Bill Jastram
> >Cc: Perl List
> >Subject: Re: Align Text
> >
> >
> >On Dec 19, 2003, at 8:29 AM, Bill Jastram wrote:
> >> >where I> >> James:
> >>
> >> A coupe of things.
> >>
> >> #1. Pardon my ignorance, but I'm not sure how to use the list> >way> >> found the reply button to e-mail you directly. I could not find a> >appreciated.> >> to
> >> add to the already existing 'thread'. Your help would be
> >
> >The list is just another address you can send your messages too:
> >
> >beginners@perl.org
> >
> >A lot of people just hit "Reply All" if their mail client has such a
> >button. In the case of this message, that would send your answer to
> >me
> >and the list.
> >> >> >> #2. The challenge with the use of %20s is that in the next row of
> >> records
> >> the names will not be aligned (flush left) with the row above, if the
> >> names are not the same length. This is my dilemma.
> >Correct, but they will be aligned flush right. For flush left, we just
> >change the pattern a little:
> >> >James Edward Gray> > > perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
> >
> >Does that answer your question?
> >
> >James
> >
> >
> >--
> >To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> >For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> ><http://learn.perl.org/> <http://learn.perl.org/first-response>
> >
> >
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
[url]http://photos.yahoo.com/[/url]
Jeff Westman Guest
-
James Edward Gray II #8
Re: Align Text
On Dec 19, 2003, at 12:04 PM, Bill Jastram wrote:
No sir, I'm afraid I don't:> We're getting closer. But lets say the first name of the first field
> in the first row is 'Bill'. And the first name of the first field in
> the second row is 'Lanette'. This command will not compensate for the
> difference in the length of the two first names. So, the alignment of
> the second fields in each row will not be correct. And so on ...
>
> Does this help you understand where I'm headed?
perl -e '@names = (["Bill", "Gray"],["Lanette", "Jones"]); printf
"%-20s %-20s\n", $$_[0], $$_[1] foreach @names'
Bill Gray
Lanette Jones
The above DOES line up correctly, with a fixed width font.
If you need to adjust the size of the fields based on the names, you'll
need to make two passes. First, find the longest lengths. Then print.
printf() is doing the right thing though, as you can see above.
James
James Edward Gray II Guest
-
Tom Kinzer #9
RE: Align Text
say wha? show me, please.
-----Original Message-----
From: Bill Jastram [mailto:tfc4@cybcon.com]
Sent: Friday, December 19, 2003 10:04 AM
To: Tom Kinzer
Cc: James Edward Gray II; Perl List
Subject: RE: Align Text
We're getting closer. But lets say the first name of the first field in the
first row is 'Bill'. And the first name of the first field in the second row
is 'Lanette'. This command will not compensate for the difference in the
length of the two first names. So, the alignment of the second fields in
each row will not be correct. And so on ...
Does this help you understand where I'm headed?
Bill
__________________________________________________
-->yes, and if that won't work for you, check out the 'format' command,
>you can
>do lots of slick stuff with it, like centering.
>
>-Tom Kinzer
>
>-----Original Message-----
>From: James Edward Gray II [mailto:james@grayproductions.net]
>Sent: Friday, December 19, 2003 9:16 AM
>To: Bill Jastram
>Cc: Perl List
>Subject: Re: Align Text
>
>
>On Dec 19, 2003, at 8:29 AM, Bill Jastram wrote:
>>where I>> James:
>>
>> A coupe of things.
>>
>> #1. Pardon my ignorance, but I'm not sure how to use the list>way>> found the reply button to e-mail you directly. I could not find a>appreciated.>> to
>> add to the already existing 'thread'. Your help would be
>
>The list is just another address you can send your messages too:
>
>beginners@perl.org
>
>A lot of people just hit "Reply All" if their mail client has such a
>button. In the case of this message, that would send your answer to
>me
>and the list.
>>>> #2. The challenge with the use of %20s is that in the next row of
>> records
>> the names will not be aligned (flush left) with the row above, if the
>> names are not the same length. This is my dilemma.
>Correct, but they will be aligned flush right. For flush left, we just
>change the pattern a little:
>>James Edward Gray> > perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
>
>Does that answer your question?
>
>James
>
>
>--
>To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
>For additional commands, e-mail: [email]beginners-help@perl.org[/email]
><http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Tom Kinzer Guest
-
Rob Dixon #10
Re: Align Text
Bill Jastram wrote:
Hi Bill.> We're getting closer. But lets say the first name of the first
> field in the first row is 'Bill'. And the first name of the
> first field in the second row is 'Lanette'. This command will
> not compensate for the difference in the length of the two
> first names. So, the alignment of the second fields in each
> row will not be correct. And so on ...
>
> Does this help you understand where I'm headed?
What James wrote still applies I think. See the code below,
which does what I think you want?
HTH,
Rob
foreach ('Bill Jastram', 'Lanette Smith') {
printf "%-20s %-20s\n", split;
}
**OUTPUT
Bill Jastram
Lanette Smith
Rob Dixon Guest
-
Eric Walker #11
Re: Align Text
I have use the FORMAT function in perl. Its pretty nice.
perldoc -f format.
Hope that helps
perlknucklehead
On Fri, 2003-12-19 at 14:20, Rob Dixon wrote:
Bill Jastram wrote:
Hi Bill.> We're getting closer. But lets say the first name of the first
> field in the first row is 'Bill'. And the first name of the
> first field in the second row is 'Lanette'. This command will
> not compensate for the difference in the length of the two
> first names. So, the alignment of the second fields in each
> row will not be correct. And so on ...
>
> Does this help you understand where I'm headed?
What James wrote still applies I think. See the code below,
which does what I think you want?
HTH,
Rob
foreach ('Bill Jastram', 'Lanette Smith') {
printf "%-20s %-20s\n", split;
}
**OUTPUT
Bill Jastram
Lanette Smith
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Eric Walker Guest
-
R. Joseph Newton #12
Re: Align Text
Bill Jastram wrote:
Yes. And printf, sprintf, or the perl format/write function pair should be able to help with this. They should at least give you a starting point, because there are many ways to accomplish this task. I prefer the printf, mostly because I'd rather just say expplictly how many spaces each field should occupy. Others prefer using the format> We're getting closer. But lets say the first name of the first field in the first row is 'Bill'. And the first name of the first field in the second row is 'Lanette'. This command will not compensate for the difference in the length of the two first names. So, the alignment of the second fields in each row will not be correct. And so on ...
>
> Does this help you understand where I'm headed?
>
> Bill
function to provide a parameter for write(), because they see it as WYSIWYG. Try them both and see which you feel more comfortable with for any given context.
Joseph
R. Joseph Newton Guest
-
Bill Jastram #13
Re: Align Text
James:
Thanks for the sample and I agree it does work.
How can I create an array of just the first names for a file?
This is what I have so far:
__________________________________________________
open (A, "testing.txt");
@A = <A>;
foreach ($n = 0; $n<10; $n++)
{
#Split each record into its fields
$item = $A[$n];
@addArray = split( "\t", $item); #Splits each line into its tab fields
$first = $addArray[0]; #Breaks down array into proper fields
$last = $addArray[1];
$add = $addArray[2];
$city = $addArray[3];
$state = $addArray[4];
$zip = $addArray[5];
printf "\n%s %s\n%s\n%s %s %s\n", $first, $last, $add, $city, $state,
$zip;
_________________________________________________
What it seems I need now is to create arrays for each of the fields, so I
can proceed to make three columns of labels.
Thanks for any assistance you can give me with this.
Bill J.
__________________________________________________
On Fri, 19 Dec 2003, James Edward Gray II wrote:
> On Dec 19, 2003, at 12:04 PM, Bill Jastram wrote:
>>> > We're getting closer. But lets say the first name of the first field
> > in the first row is 'Bill'. And the first name of the first field in
> > the second row is 'Lanette'. This command will not compensate for the
> > difference in the length of the two first names. So, the alignment of
> > the second fields in each row will not be correct. And so on ...
> >
> > Does this help you understand where I'm headed?
> No sir, I'm afraid I don't:
>
> perl -e '@names = (["Bill", "Gray"],["Lanette", "Jones"]); printf
> "%-20s %-20s\n", $$_[0], $$_[1] foreach @names'
>
> Bill Gray
> Lanette Jones
>
> The above DOES line up correctly, with a fixed width font.
>
> If you need to adjust the size of the fields based on the names, you'll
> need to make two passes. First, find the longest lengths. Then print.
>
> printf() is doing the right thing though, as you can see above.
>
> James
>Bill Jastram Guest
-
James Edward Gray II #14
Re: Align Text
On Dec 21, 2003, at 10:47 PM, Bill Jastram wrote:
Let's take a look at what you have first.> James:
>
> Thanks for the sample and I agree it does work.
>
> How can I create an array of just the first names for a file?
>
> This is what I have so far:
You're missing two very important lines right here:
use strict;
use warnings;
These promise Perl you'll play by the good programmer rules, so it can
help you find problems. That's a good deal.
Always check if an open succeeded. There are plenty of reasons it may> open (A, "testing.txt");
not.
Also, you'll generally stay more sane if you use better names for file
handle/variables than A.
open CONTACTS, 'testing.txt' or die "File error: $!";
my @A = <CONTACTS>;> @A = <A>;
We're reading the whole file here, but you only need one line at a
time. We can do better.
These two lines can be simplified:> foreach ($n = 0; $n<10; $n++)
> {
>
> #Split each record into its fields
> $item = $A[$n];
for my $item (@A[0..9]) { # if you really only wanted the first ten
lines
# or...
for my $item (@A) { # if you wanted them all
my($first, $last, $add, $city, $state, $zip) = split /\t/, $item;> @addArray = split( "\t", $item); #Splits each line into its tab fields
> $first = $addArray[0]; #Breaks down array into proper fields
> $last = $addArray[1];
> $add = $addArray[2];
> $city = $addArray[3];
> $state = $addArray[4];
> $zip = $addArray[5];
Let's try something simpler:> printf "\n%s %s\n%s\n%s %s %s\n", $first, $last, $add, $city, $state,
> $zip;
> _________________________________________________
#!/usr/bin/perl
# use with:
# perl this_script_name testing.txt
use strict;
use warnings;
while (<>) { # process args line by line
printf "\n%s %s\n%s\n%s %s %s\n", split /\t/, $_;
}
__END__
That's a little trickier, but let's see if we can keep it pretty simple:> What it seems I need now is to create arrays for each of the fields,
> so I
> can proceed to make three columns of labels.
#!/usr/bin/perl
# use with:
# perl this_script_name testing.txt
use strict;
use warnings;
my(@col1, @col2, @col3);
my $col = 1;
while (<>) {
if ($col == 1) { push @col1, $_ }
elsif ($col == 2) { push @col, $_ }
else {
push @col3, $_;
$col = 1;
next;
}
$col++;
}
# that should load @col1, @col2 and @col3
# can you come up with an output loop for them that goes here?
__END__
Does that help you along?
James
James Edward Gray II Guest
-
Bill Jastram #15
Re: Align Text
Wow! That's a lot to take in, but consider it, and I'll get back
to you with the results of my considerations.
Thanks much.
Bill J.
__________________________________________________
On Mon, 22 Dec 2003, James Edward Gray II wrote:
> On Dec 21, 2003, at 10:47 PM, Bill Jastram wrote:
>>> > James:
> >
> > Thanks for the sample and I agree it does work.
> >
> > How can I create an array of just the first names for a file?
> >
> > This is what I have so far:
> Let's take a look at what you have first.
>
> You're missing two very important lines right here:
>
> use strict;
> use warnings;
>
> These promise Perl you'll play by the good programmer rules, so it can
> help you find problems. That's a good deal.
>>> > open (A, "testing.txt");
> Always check if an open succeeded. There are plenty of reasons it may
> not.
>
> Also, you'll generally stay more sane if you use better names for file
> handle/variables than A.
>
> open CONTACTS, 'testing.txt' or die "File error: $!";
>>> > @A = <A>;
> my @A = <CONTACTS>;
>
> We're reading the whole file here, but you only need one line at a
> time. We can do better.
>>> > foreach ($n = 0; $n<10; $n++)
> > {
> >
> > #Split each record into its fields
> > $item = $A[$n];
> These two lines can be simplified:
>
> for my $item (@A[0..9]) { # if you really only wanted the first ten
> lines
>
> # or...
>
> for my $item (@A) { # if you wanted them all
>>> > @addArray = split( "\t", $item); #Splits each line into its tab fields
> > $first = $addArray[0]; #Breaks down array into proper fields
> > $last = $addArray[1];
> > $add = $addArray[2];
> > $city = $addArray[3];
> > $state = $addArray[4];
> > $zip = $addArray[5];
> my($first, $last, $add, $city, $state, $zip) = split /\t/, $item;
>>> > printf "\n%s %s\n%s\n%s %s %s\n", $first, $last, $add, $city, $state,
> > $zip;
> > _________________________________________________
> Let's try something simpler:
>
> #!/usr/bin/perl
>
> # use with:
> # perl this_script_name testing.txt
>
> use strict;
> use warnings;
>
> while (<>) { # process args line by line
> printf "\n%s %s\n%s\n%s %s %s\n", split /\t/, $_;
> }
>
> __END__
>>> > What it seems I need now is to create arrays for each of the fields,
> > so I
> > can proceed to make three columns of labels.
> That's a little trickier, but let's see if we can keep it pretty simple:
>
> #!/usr/bin/perl
>
> # use with:
> # perl this_script_name testing.txt
>
> use strict;
> use warnings;
>
> my(@col1, @col2, @col3);
>
> my $col = 1;
> while (<>) {
> if ($col == 1) { push @col1, $_ }
> elsif ($col == 2) { push @col, $_ }
> else {
> push @col3, $_;
> $col = 1;
> next;
> }
> $col++;
> }
>
> # that should load @col1, @col2 and @col3
> # can you come up with an output loop for them that goes here?
>
> __END__
>
> Does that help you along?
>
> James
>Bill Jastram Guest
-
Bill Jastram #16
Re: Align Text
After taking a look at your last suggested script I have a short
question:
At what point does this script read in information from the
source file 'testing.txt'?
Is it during the 'while (<>)' statement? If so what would the syntax be?
Thanks again for your help.
Bill J.
__________________________________________________
On Mon, 22 Dec 2003, James Edward Gray II wrote:
> On Dec 21, 2003, at 10:47 PM, Bill Jastram wrote:
>>> > James:
> >
> > Thanks for the sample and I agree it does work.
> >
> > How can I create an array of just the first names for a file?
> >
> > This is what I have so far:
> Let's take a look at what you have first.
>
> You're missing two very important lines right here:
>
> use strict;
> use warnings;
>
> These promise Perl you'll play by the good programmer rules, so it can
> help you find problems. That's a good deal.
>>> > open (A, "testing.txt");
> Always check if an open succeeded. There are plenty of reasons it may
> not.
>
> Also, you'll generally stay more sane if you use better names for file
> handle/variables than A.
>
> open CONTACTS, 'testing.txt' or die "File error: $!";
>>> > @A = <A>;
> my @A = <CONTACTS>;
>
> We're reading the whole file here, but you only need one line at a
> time. We can do better.
>>> > foreach ($n = 0; $n<10; $n++)
> > {
> >
> > #Split each record into its fields
> > $item = $A[$n];
> These two lines can be simplified:
>
> for my $item (@A[0..9]) { # if you really only wanted the first ten
> lines
>
> # or...
>
> for my $item (@A) { # if you wanted them all
>>> > @addArray = split( "\t", $item); #Splits each line into its tab fields
> > $first = $addArray[0]; #Breaks down array into proper fields
> > $last = $addArray[1];
> > $add = $addArray[2];
> > $city = $addArray[3];
> > $state = $addArray[4];
> > $zip = $addArray[5];
> my($first, $last, $add, $city, $state, $zip) = split /\t/, $item;
>>> > printf "\n%s %s\n%s\n%s %s %s\n", $first, $last, $add, $city, $state,
> > $zip;
> > _________________________________________________
> Let's try something simpler:
>
> #!/usr/bin/perl
>
> # use with:
> # perl this_script_name testing.txt
>
> use strict;
> use warnings;
>
> while (<>) { # process args line by line
> printf "\n%s %s\n%s\n%s %s %s\n", split /\t/, $_;
> }
>
> __END__
>>> > What it seems I need now is to create arrays for each of the fields,
> > so I
> > can proceed to make three columns of labels.
> That's a little trickier, but let's see if we can keep it pretty simple:
>
> #!/usr/bin/perl
>
> # use with:
> # perl this_script_name testing.txt
>
> use strict;
> use warnings;
>
> my(@col1, @col2, @col3);
>
> my $col = 1;
> while (<>) {
> if ($col == 1) { push @col1, $_ }
> elsif ($col == 2) { push @col, $_ }
> else {
> push @col3, $_;
> $col = 1;
> next;
> }
> $col++;
> }
>
> # that should load @col1, @col2 and @col3
> # can you come up with an output loop for them that goes here?
>
> __END__
>
> Does that help you along?
>
> James
>Bill Jastram Guest
-
James Edward Gray II #17
Re: Align Text
On Dec 23, 2003, at 12:17 AM, Bill Jastram wrote:
Yes, you've got it.> After taking a look at your last suggested script I have a short
> question:
>
> At what point does this script read in information from the
> source file 'testing.txt'?
>
> Is it during the 'while (<>)' statement? If so what would the syntax
> be?
while (<>) {
}
is a construct for writing Unix-like filter apps. It reads one line at
a time, from all the files passed as args to the script or STDIN, if
none are given. This is extremely handy.
For example, if you wanted to combine two text files for your mailing
labels, you would just need to change how you call it:
perl script_name testing.txt other_file.txt
....or even...
perl script_name *.txt
....for all the .txt files in the current working directory.
Hope that helps.
James
James Edward Gray II Guest
-
Bill Jastram #18
Re: Align Text
Yes, this helps tremendously. Actually, your suggestions helped me make,
what for me is, a quantum leap in the use of Perl.
I am stuck, however, on the loop you suggested to output the processed
@col arrays. I understand how you loaded them. And that, apparently, they
are ready to be output. But the closest I've come getting anything out of
them is the following:
__________________________________________________
#!/usr/bin/perl
# use with:
# perl jamescolumnsample testing.txt
use strict;
use warnings;
#open CON, 'testing.txt' or die "File error: $!";
#my @CON = <CON> ;
my(@col1, @col2, @col3);
my $col = 1;
while (<>) {
if ($col == 1) { push @col1, $_ }
elsif ($col == 2) { push @col2, $_ }
else {
push @col3, $_;
$col = 1;
next;
}
$col++;
}
# that should load @col1, @col2 and @col3
# can you come up with an output loop for them that goes here?
my($first, $last, $add, $city, $state, $zip) = split /\t/, $col1[$col];
printf "\n%s %s\n%s\n%s %s %s\n", $first, $last, $add, $city, $state,
$zip;
Any futher help with this would be appreciated.
Bill J.
__________________________________________________
On Tue, 23 Dec 2003, James Edward Gray II wrote:
> On Dec 23, 2003, at 12:17 AM, Bill Jastram wrote:
>>> > After taking a look at your last suggested script I have a short
> > question:
> >
> > At what point does this script read in information from the
> > source file 'testing.txt'?
> >
> > Is it during the 'while (<>)' statement? If so what would the syntax
> > be?
> Yes, you've got it.
>
> while (<>) {
>
> }
>
> is a construct for writing Unix-like filter apps. It reads one line at
> a time, from all the files passed as args to the script or STDIN, if
> none are given. This is extremely handy.
>
> For example, if you wanted to combine two text files for your mailing
> labels, you would just need to change how you call it:
>
> perl script_name testing.txt other_file.txt
>
> ...or even...
>
> perl script_name *.txt
>
> ...for all the .txt files in the current working directory.
>
> Hope that helps.
>
> James
>Bill Jastram Guest
-
James Edward Gray II #19
Re: Align Text
On Dec 23, 2003, at 11:29 PM, Bill Jastram wrote:
Good news. Always happy to help.> Yes, this helps tremendously. Actually, your suggestions helped me
> make,
> what for me is, a quantum leap in the use of Perl.
Well, let's see if we can come up with something...> I am stuck, however, on the loop you suggested to output the processed
> @col arrays. I understand how you loaded them. And that, apparently,
> they
> are ready to be output. But the closest I've come getting anything out
> of
> them is the following:
I forgot to remove the newlines here and we probably should:> #!/usr/bin/perl
>
> # use with:
> # perl jamescolumnsample testing.txt
>
> use strict;
> use warnings;
>
> #open CON, 'testing.txt' or die "File error: $!";
>
> #my @CON = <CON> ;
>
> my(@col1, @col2, @col3);
>
> my $col = 1;
> while (<>) {
chomp;
# col1 will be the last to empty, so loop until it's gone> if ($col == 1) { push @col1, $_ }
> elsif ($col == 2) { push @col2, $_ }
> else {
> push @col3, $_;
> $col = 1;
> next;
> }
> $col++;
> }
>
> # that should load @col1, @col2 and @col3
> # can you come up with an output loop for them that goes here?
while (@col1) {
my(@names, @addresses, @cities); # make lists for the output lines
# fill those lists
foreach (shift(@col1), shift(@col2), shift(@col3)) {
next unless defined $_;
my($first, $last, $address, $city, $state, $zip) = split /\t/, $_;
push @names, "$first $last";
push @addresses, $address;
push @cities, "$city, $state $zip";
}
# print one row of contacts
foreach (\@names, \@addresses, \@cities) {
printf join(' ', ('%-30s') x scalar(@$_)) . "\n", @$_;
}
print "\n" if @col1; # add a separator
} # rinse, repeat...
__END__
Unfortunately, I'm not where I can test code this morning, so I'll have
to just hope that works. Let me know if I made a mistake though and
I'll fix it as soon as I'm back at a compiler.
Hope that helps you along, Bill. Keep working at it.
James
James Edward Gray II Guest
-
Bill Jastram #20
Re: Align Text
Bingo! James. That takes care of label issue.
I was even able to make it three columns instead of three. And change the
space between columns. You've obviously been at this for a while and use
the tightest code concepts I've seen.
By the way, how would you 'search' the incoming file to restrict what gets
displayed? i.e. display only the labels for the Johnson's?
Thanks James, you've been of meg-assistance.
Bill
__________________________________________________
On Wed, 24 Dec 2003, James Edward Gray II wrote:
> On Dec 23, 2003, at 11:29 PM, Bill Jastram wrote:
>>> > Yes, this helps tremendously. Actually, your suggestions helped me
> > make,
> > what for me is, a quantum leap in the use of Perl.
> Good news. Always happy to help.
>>> > I am stuck, however, on the loop you suggested to output the processed
> > @col arrays. I understand how you loaded them. And that, apparently,
> > they
> > are ready to be output. But the closest I've come getting anything out
> > of
> > them is the following:
> Well, let's see if we can come up with something...
>>> > #!/usr/bin/perl
> >
> > # use with:
> > # perl jamescolumnsample testing.txt
> >
> > use strict;
> > use warnings;
> >
> > #open CON, 'testing.txt' or die "File error: $!";
> >
> > #my @CON = <CON> ;
> >
> > my(@col1, @col2, @col3);
> >
> > my $col = 1;
> > while (<>) {
> I forgot to remove the newlines here and we probably should:
>
> chomp;
>>> > if ($col == 1) { push @col1, $_ }
> > elsif ($col == 2) { push @col2, $_ }
> > else {
> > push @col3, $_;
> > $col = 1;
> > next;
> > }
> > $col++;
> > }
> >
> > # that should load @col1, @col2 and @col3
> > # can you come up with an output loop for them that goes here?
> # col1 will be the last to empty, so loop until it's gone
> while (@col1) {
> my(@names, @addresses, @cities); # make lists for the output lines
> # fill those lists
> foreach (shift(@col1), shift(@col2), shift(@col3)) {
> next unless defined $_;
> my($first, $last, $address, $city, $state, $zip) = split /\t/, $_;
> push @names, "$first $last";
> push @addresses, $address;
> push @cities, "$city, $state $zip";
> }
>
> # print one row of contacts
> foreach (\@names, \@addresses, \@cities) {
> printf join(' ', ('%-30s') x scalar(@$_)) . "\n", @$_;
> }
> print "\n" if @col1; # add a separator
> } # rinse, repeat...
>
> __END__
>
> Unfortunately, I'm not where I can test code this morning, so I'll have
> to just hope that works. Let me know if I made a mistake though and
> I'll fix it as soon as I'm back at a compiler.
>
> Hope that helps you along, Bill. Keep working at it.
>
> James
>Bill Jastram Guest



Reply With Quote

