Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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
    4. 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...
    5. 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...
  3. #2

    Default Re: Align text

    \On Dec 16, 2003, at 1:39 PM, Hemond, Steve wrote:
    > 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?
    You're looking for printf(). See if this documentation fixes you up:

    perldoc -f printf

    perldoc -f sprintf

    James

    James Edward Gray II Guest

  4. #3

    Default Re: Align Text

    On Dec 18, 2003, at 12:03 AM, Bill Jastram wrote:
    > James:
    I'm happy to help, but keep your replies on the list so we can all
    learn/help.
    > Can printf set the value in spaces of tab (\t)? I need to do three
    > column
    > output with perl for mailing labels.
    You bet:
    >perl -e 'printf "%20s %20s %20s\n", "James", "Edward", "Gray"'
    James Edward Gray

    Three columns of 20 character width right there. Does that answer your
    question?

    James

    James Edward Gray II Guest

  5. #4

    Default Re: Align Text

    On Dec 19, 2003, at 8:29 AM, Bill Jastram wrote:
    > 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.
    The list is just another address you can send your messages too:

    [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.
    > #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:
    > perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
    James Edward Gray

    Does that answer your question?

    James

    James Edward Gray II Guest

  6. #5

    Default 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:
    > 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.
    The list is just another address you can send your messages too:

    [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.
    > #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:
    > perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
    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

  7. #6

    Default 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:
    >
    >> 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.
    >
    >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:
    >
    > > perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
    >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

  8. #7

    Default 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:
    > >
    > >> 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.
    > >
    > >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:
    > >
    > > > perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
    > >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

  9. #8

    Default Re: Align Text

    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

    James Edward Gray II Guest

  10. #9

    Default 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:
    >
    >> 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.
    >
    >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:
    >
    > > perl -e 'printf "%-20s %-20s %-20s\n", "James", "Edward", "Gray"'
    >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

  11. #10

    Default Re: Align Text

    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?
    Hi Bill.

    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

  12. #11

    Default 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:
    > 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?
    Hi Bill.

    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

  13. #12

    Default Re: Align Text

    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?
    >
    > Bill
    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
    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

  14. #13

    Default 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

  15. #14

    Default Re: Align Text

    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

    James Edward Gray II Guest

  16. #15

    Default 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

  17. #16

    Default 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

  18. #17

    Default Re: Align Text

    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

    James Edward Gray II Guest

  19. #18

    Default 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

  20. #19

    Default Re: Align Text

    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

    James Edward Gray II Guest

  21. #20

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139