column format -easier way of doing it 
Author Message
 column format -easier way of doing it

Hello there,

I use the following script to display addressbook.LDIF in two columns.
This works, but there must be an easier way of adjusting the start of
the second column, I just cannot find it:

#!/usr/bin/perl -w

use strict;    

my ($infile, $outfile, $mailrc, $name, $strlength);

# open(STDERR, ">&STDOUT")            or die "Can't dup stdout: $!";

$infile = 'C:\aa\AddressBook.ldif' ;
$outfile = 'C:\aa\addresslist.txt' ;

open(MAILRC, "< $infile")
    or die "Cannot read mailrc\n";

open(OUT, "> $outfile")
          or die "Cannot open addresslist-out.txt\n";

while( <MAILRC> ){

        if(s/^dn: cn=(.+?),mail=(.+?)/$1\t$2/) {
        $name = $1 ;
        $strlength = length ($name) ;

        if ($strlength <= 10) {
        s/(.+?)\t(.+?)/$1                    $2/ ;  # 20 spaces
        }

        if ($strlength == 11) {
       s/(.+?)\t(.+?)/$1                   $2/ ;  # 19 spaces
        }

        # Snipped, this goes on to $strlength == 28 and 2 spaces for padding

        print OUT "$_" ;
        }

Quote:
}

close MAILRC    || die "cannot close $infile: $!";
close (OUT)             || die "cannot close $outfile:     $!";

---  Fred



Sun, 20 Feb 2005 06:45:38 GMT  
 column format -easier way of doing it

Quote:

> Hello there,

> I use the following script to display addressbook.LDIF in two columns.
> This works, but there must be an easier way of adjusting the start of
> the second column, I just cannot find it:

> #!/usr/bin/perl    -w

> use strict;        

> my ($infile, $outfile, $mailrc, $name, $strlength);

> # open(STDERR, ">&STDOUT")            or die "Can't dup stdout: $!";

> $infile = 'C:\aa\AddressBook.ldif' ;
> $outfile = 'C:\aa\addresslist.txt' ;

> open(MAILRC, "< $infile")
>     or die "Cannot read mailrc\n";

> open(OUT, "> $outfile")
>      or die "Cannot open addresslist-out.txt\n";

> while( <MAILRC> ){

>    if(s/^dn: cn=(.+?),mail=(.+?)/$1\t$2/) {
>    $name = $1 ;
>    $strlength = length ($name) ;

>    if ($strlength <= 10) {
>         s/(.+?)\t(.+?)/$1                    $2/ ;  # 20 spaces
>    }

>    if ($strlength == 11) {
>        s/(.+?)\t(.+?)/$1                   $2/ ;  # 19 spaces
>    }

>    # Snipped, this goes on to $strlength == 28 and 2 spaces for padding

>    print OUT "$_" ;
>    }
> }

> close MAILRC       || die "cannot close $infile: $!";
> close (OUT)                || die "cannot close $outfile:     $!";

> ---  Fred

perldoc -f sprintf

Bodo



Sun, 20 Feb 2005 07:48:25 GMT  
 column format -easier way of doing it

Quote:

> Hello there,

> I use the following script to display addressbook.LDIF in two columns.
> This works, but there must be an easier way of adjusting the start of
> the second column, I just cannot find it:

Or look at: perldoc perlform

The R in perl stands for Reporting, and perl formats can handle with
titles, pagelengths etc.

Rolf Schaufelberger



Sun, 20 Feb 2005 10:28:24 GMT  
 column format -easier way of doing it

Quote:

> I use the following script to display addressbook.LDIF in two columns.
> This works, but there must be an easier way of adjusting the start of
> the second column, I just cannot find it:

It is illogical of you to post so much code which
is unrelated to your question.

(snipped)

Quote:
>         if(s/^dn: cn=(.+?),mail=(.+?)/$1\t$2/) {
>         $name = $1 ;
>         $strlength = length ($name) ;
>         if ($strlength <= 10) {
>         s/(.+?)\t(.+?)/$1                    $2/ ;  # 20 spaces
>         }
>         if ($strlength == 11) {
>        s/(.+?)\t(.+?)/$1                   $2/ ;  # 19 spaces
>         }
>         # Snipped, this goes on to $strlength == 28 and 2 spaces for padding

Your first code line, of this snipped display, is most
inconsistent with your remaining code lines. Your use
of a less than operator destroys your formatting.

Clearly your logic is less than adequate.

A presumption is made your $name length never exceeds
twenty-eight based upon your last parameter.

Work towards developing an ability to write articles
which are clear, concise and coherent along with being
free of unrelated coding.

Purl Gurl

#!perl

print "Content-type: text/plain\n\n";

while(<DATA>)
 {
  if (index ($_, "dn: cn") == 0)
   {
    substr ($_, 0, 7, "");
    $var_1 = substr ($_, 0, index ($_, ","), "");
    $var_2 = substr ($_, 6);
    print $var_1, ' ' x (30 - length ($var_1)), $var_2;
   }
 }

__DATA__
dn: cn=Purl Gurl,mail=Rocks
dn: cn=And,mail=Rolls!
dn: cn=Oh,mail=Yes
dn: cn=She,mail=Does!

PRINTED RESULTS:
________________

Purl Gurl                     Rocks
And                           Rolls!
Oh                            Yes
She                           Does!



Sun, 20 Feb 2005 18:51:58 GMT  
 column format -easier way of doing it

Quote:


> > I use the following script to display addressbook.LDIF in two columns.
> > This works, but there must be an easier way of adjusting the start of
> > the second column, I just cannot find it:

> It is illogical of you to post so much code which
> is unrelated to your question.

> (snipped)

> >         if(s/^dn: cn=(.+?),mail=(.+?)/$1\t$2/) {
> >         $name = $1 ;
> >         $strlength = length ($name) ;

> >         if ($strlength <= 10) {
> >         s/(.+?)\t(.+?)/$1                    $2/ ;  # 20 spaces
> >         }

> >         if ($strlength == 11) {
> >        s/(.+?)\t(.+?)/$1                   $2/ ;  # 19 spaces
> >         }

> >         # Snipped, this goes on to $strlength == 28 and 2 spaces for padding

> Your first code line, of this snipped display, is most
> inconsistent with your remaining code lines. Your use
> of a less than operator destroys your formatting.

> Clearly your logic is less than adequate.

> A presumption is made your $name length never exceeds
> twenty-eight based upon your last parameter.

> Work towards developing an ability to write articles
> which are clear, concise and coherent along with being
> free of unrelated coding.

> Purl Gurl

> #!perl

> print "Content-type: text/plain\n\n";

> while(<DATA>)
>  {
>   if (index ($_, "dn: cn") == 0)
>    {
>     substr ($_, 0, 7, "");
>     $var_1 = substr ($_, 0, index ($_, ","), "");
>     $var_2 = substr ($_, 6);
>     print $var_1, ' ' x (30 - length ($var_1)), $var_2;
>    }
>  }

> __DATA__
> dn: cn=Purl Gurl,mail=Rocks
> dn: cn=And,mail=Rolls!
> dn: cn=Oh,mail=Yes
> dn: cn=She,mail=Does!

> PRINTED RESULTS:
> ________________

> Purl Gurl                     Rocks
> And                           Rolls!
> Oh                            Yes
> She                           Does!

~~~~~~~~~~~~~

Purl, your comment was devastating, but the code you provided works
fine for me. Now I have only to try to understand it... Thank you very
much!

--- Fred



Sun, 20 Feb 2005 23:37:21 GMT  
 column format -easier way of doing it

Quote:


> > Hello there,

> > I use the following script to display addressbook.LDIF in two columns.
> > This works, but there must be an easier way of adjusting the start of
> > the second column, I just cannot find it:

> > #!/usr/bin/perl       -w

> > use strict;

> > my ($infile, $outfile, $mailrc, $name, $strlength);

> > # open(STDERR, ">&STDOUT")            or die "Can't dup stdout: $!";

> > $infile = 'C:\aa\AddressBook.ldif' ;
> > $outfile = 'C:\aa\addresslist.txt' ;

> > open(MAILRC, "< $infile")
> >     or die "Cannot read mailrc\n";

> > open(OUT, "> $outfile")
> >         or die "Cannot open addresslist-out.txt\n";

> > while( <MAILRC> ){

> >       if(s/^dn: cn=(.+?),mail=(.+?)/$1\t$2/) {
> >       $name = $1 ;
> >       $strlength = length ($name) ;

> >       if ($strlength <= 10) {
> >         s/(.+?)\t(.+?)/$1                    $2/ ;  # 20 spaces
> >       }

> >       if ($strlength == 11) {
> >        s/(.+?)\t(.+?)/$1                   $2/ ;  # 19 spaces
> >       }

> >       # Snipped, this goes on to $strlength == 28 and 2 spaces for padding

> >       print OUT "$_" ;
> >       }
> > }

> > close MAILRC  || die "cannot close $infile: $!";
> > close (OUT)           || die "cannot close $outfile:  $!";

> > ---  Fred

> perldoc -f sprintf

> Bodo

~~~~~~~~~~~~

Bodo, your answer was not much help. No answer at all would have been
better...

--- Fred



Sun, 20 Feb 2005 23:40:55 GMT  
 column format -easier way of doing it

(snipped)

Quote:
> > Your first code line, of this snipped display, is most
> > inconsistent with your remaining code lines. Your use
> > of a less than operator destroys your formatting.
> > Clearly your logic is less than adequate.
> > A presumption is made your $name length never exceeds
> > twenty-eight based upon your last parameter.
> > Work towards developing an ability to write articles
> > which are clear, concise and coherent along with being
> > free of unrelated coding.
> Purl, your comment was devastating,

  "your comments are"

My comments are still of an annoying nature;
they have not changed. Your "was" indicates
my comment(s) is no longer annoying.

Actually my comments are quite merciful
considering I am an English teacher and
an English professor.

I am also a science teacher. Can't get
much worse, yes? Well, sometimes I do
teach a bit of mathematics.

Quote:
> but the code you provided works fine for me.
> Now I have only to try to understand it...

Pretty simple really, save for one operator.

Substring simply grabs your variables. There is
a lot of documentation on using substring.

  print $var_1, ' ' x (30 - length ($var_1)), $var_2;

                    ^  x marks the spot

This one operator, my  " x " within my print,
I have never found any direct reference to this
x operator in Perl documentation nor any
Perl books.  It is a simple multiplier for
printing characters.

This appears to be an undocumented operator.

If you find direct documentation on this operator,
please cite a reference or link.

Don't expect the character multiplier to work with
all mathematical operators, such as an exponent or
Scientific Notation.

Purl Gurl

#!perl
print "Purl Gurl Rocks!\n" x (10**8);

#!perl
print "Purl Gurl Rocks!\n" x (10**10);

#!perl
print "Heh, heh... heh";



Mon, 21 Feb 2005 02:57:11 GMT  
 column format -easier way of doing it

Quote:

> This one operator, my  " x " within my print,
> I have never found any direct reference to this
> x operator in Perl documentation nor any
> Perl books.  

> This appears to be an undocumented operator.

It's documented in perl5.

  <http://mirrors.develooper.com/perl/really-ancient-perls>

--
Steve

perldoc -qa.j | perl -lpe '($_)=m("(.*)")'



Mon, 21 Feb 2005 03:59:43 GMT  
 column format -easier way of doing it

Quote:


> > This one operator, my  " x " within my print,
> > I have never found any direct reference to this
> > x operator in Perl documentation nor any
> > Perl books.
> > This appears to be an undocumented operator.
> It's documented in perl5.
>   <http://mirrors.develooper.com/perl/really-ancient-perls>

There is no documentation available at this
site you cite.

My presumption is you have not bothered
to examine this link of yours.

Purl Gurl



Mon, 21 Feb 2005 04:55:32 GMT  
 column format -easier way of doing it

Quote:


> > This one operator, my  " x " within my print,
> > I have never found any direct reference to this
> > x operator in Perl documentation nor any
> > Perl books.
> > This appears to be an undocumented operator.
> It's documented in perl5.
>   <http://mirrors.develooper.com/perl/really-ancient-perls>

Although difficult to find, there are some references
to this  x  operator within Programming Perl. Depending
on which edition you have, you may or may not find it.
There is significant conflict on where documentation
is found, through my internet search.

Within Programming Perl, it appears the authors cannot
decide if the  x  operator is a string operator or
a multiplicative operator.

Programming Perl
Third Edition

1.5.2. String Operators

3.7. Multiplicative Operators

Randal refers to this operator as a "string repetition operator."

Learning Perl
Second Edition

2.4.2 Operators for Strings

Documentation is rather scant per my standards.

Purl Gurl



Mon, 21 Feb 2005 05:20:57 GMT  
 column format -easier way of doing it

Quote:



>> > This one operator, my  " x " within my print,
>> > I have never found any direct reference to this
>> > x operator in Perl documentation nor any
>> > Perl books.
>> It's documented in perl5.

>>   <http://mirrors.develooper.com/perl/really-ancient-perls>

> There is no documentation available at this
> site you cite.

There's a directory full of documentation in all the
perl5 tarballs, even the really-ancient ones.

  $ perldoc perlop  # 5.6.1
       ...

       Multiplicative Operators
       ...

       Binary "x" is the repetition operator.  In scalar context
       or if the left operand is not enclosed in parentheses, it
       returns a string consisting of the left operand repeated
       the number of times specified by the right operand.  In
       list context, if the left operand is enclosed in
       parentheses, it repeats the list.

           print '-' x 80;             # print row of dashes

           print "\t" x ($tab/8), ' ' x ($tab%8);      # tab over



--
Steve

perldoc -qa.j | perl -lpe '($_)=m("(.*)")'



Mon, 21 Feb 2005 05:59:41 GMT  
 column format -easier way of doing it

Quote:

> This one operator, my  " x " within my print, I have never found any
> direct reference to this x operator in Perl documentation nor any Perl
> books.  It is a simple multiplier for printing characters.

> This appears to be an undocumented operator.

> If you find direct documentation on this operator, please cite a
> reference or link.

From "perldoc perlop" (which is where you'd look for documentation on
operators!)

 Binary "x" is the repetition operator.  In scalar context
       or if the left operand is not enclosed in parentheses, it
       returns a string consisting of the left operand repeated
       the number of times specified by the right operand.  In
       list context, if the left operand is enclosed in parenthe-
       ses, it repeats the list.

           print '-' x 80;             # print row of dashes

           print "\t" x ($tab/8), ' ' x ($tab%8);      # tab over



This data is also online at
<http://perldoc.com/perl5.8.0/pod/perlop.html#Multiplicative-Operators>

It's also described on pages 23 (under "String Operators") and 94 (under
"Multiplicative Operators") of the third edition of "Programming Perl". I
found those by looking up "x" in the index :)

So you obviously have a strange understanding of the word "undocumented".

hth,

Dave...

--
  It was long ago and it was far away
  And it was so much better that it is today



Mon, 21 Feb 2005 07:55:14 GMT  
 column format -easier way of doing it

Quote:

>    if ($strlength <= 10) {
>        s/(.+?)\t(.+?)/$1                    $2/ ;  # 20 spaces
>    }

>    if ($strlength == 11) {
>       s/(.+?)\t(.+?)/$1                   $2/ ;  # 19 spaces
>    }

>    # Snipped, this goes on to $strlength == 28 and 2 spaces for padding

I assume what you want is a fixed length padding for the left argument?
Then

        pack 'A30', $left

or

        sprintf '%-30s', $left

could serve as a basis.

        s/(.*)\t/pack 'A30', $1/e;
or
        s/(.*)\t/sprintf '%-30s', $1/e;

One important difference in behaviour, is that pack() will cut of any
longer fields, while sprintf will just make the resultingstring longer.
It depends on what you want it to do.

--
        Bart.



Mon, 21 Feb 2005 08:37:31 GMT  
 column format -easier way of doing it

Quote:



>>>Hello there,

>>>I use the following script to display addressbook.LDIF in two columns.
>>>This works, but there must be an easier way of adjusting the start of
>>>the second column, I just cannot find it:

...snip...

Quote:
>>>---  Fred

>>perldoc -f sprintf

>>Bodo

> ~~~~~~~~~~~~

> Bodo, your answer was not much help. No answer at all would have been
> better...

> --- Fred

I apologize to have been a bit brief. I meant to say:

1. Although Purl Gurl's algorithm works for you (they always work in the
context of the respective original question), I am sure that Perl's
builtin sprintf() (or printf()) function is exactly what you need.

2. Read the documentation about this function. One of many ways to get
the documentation is typing "perldoc -f sprintf" at the command prompt.

3. From the code you posted I guessed that you just want to have $1
left-adjusted and space paded to 30 chars. The length of $2 did not seem
relevant. So you could shorten your code to say

while( <MAILRC> ){

        next unless /^dn: cn=(.+?),mail=(.+?)/;
        print OUT sprintf("%-30s$2\n", $1);
         # or printf OUT ("%-30s$2\n", $1);
         # added a newline, don't know if you need it

Quote:
}

where the percent sign indicates that the first value in the argument
list ($1 in this case) appears at this position, the hyphen indicates
that it is left-adjusted, the 30 indicates that it is space padded to 30
characters, and the s indicates that the value is interpreted as a
string. If you want output with a fixed length for $1 plus $2, $1 left
and $2 right-adjusted, using formats is a more appropriate approach.

4. If you want to have more samples of Purl Gurl-like comments and code,
read the posts of Godzilla! in this group.

Bodo



Mon, 21 Feb 2005 09:07:46 GMT  
 column format -easier way of doing it

Quote:

> Although difficult to find, there are some references
> to this  x  operator within Programming Perl.

"Difficult to find" only if you don't use the index.

Quote:
>                                               Depending
> on which edition you have, you may or may not find it.
> There is significant conflict on where documentation
> is found, through my internet search.

> Within Programming Perl, it appears the authors cannot
> decide if the  x  operator is a string operator or
> a multiplicative operator.

> Programming Perl
> Third Edition

> 1.5.2. String Operators

> 3.7. Multiplicative Operators

Hey, guess what? Perhaps it's both. It's a multiplicative operator
that takes a string as its left operand.

Quote:
> Randal refers to this operator as a "string repetition operator."

> Learning Perl
> Second Edition

> 2.4.2 Operators for Strings

That's another good description of what this operator is.

Quote:
> Documentation is rather scant per my standards.

Did you look in "perldoc perlop". That's the definitive place to get
documentation for your version of Perl. I found a complete description
of what "x" does.

hth,

Dave...



Mon, 21 Feb 2005 11:44:06 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Any better ways of doing this ?

2. **FORMAT Formatting columns/pages

3. Is there an easier way of doing this?

4. installing perl is the easiest thing I've ever done since learning how to sleep

5. Columns from a format definition?

6. Multi-column format?

7. Formatting output in columns:

8. Format a one line file to get two columns

9. Tricky column formatting/space removing question

10. multi column format

11. what I am doing wrong with the following format

12. Web Techniques Perl columns now available on-line (and more Unix Review columns)

 

 
Powered by phpBB® Forum Software