[24518] in Perl-Users-Digest
Perl-Users Digest, Issue: 6698 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 16 18:06:09 2004
Date: Wed, 16 Jun 2004 15:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 16 Jun 2004 Volume: 10 Number: 6698
Today's topics:
Re: [Qs] re "Abigails Coding Guidelines" <nospam-abuse@ilyaz.org>
Re: [Qs] re "Abigails Coding Guidelines" <usenet@morrow.me.uk>
CUPS Spool File read by perl <wschow@Comp.HKBU.Edu.HK>
Re: CUPS Spool File read by perl <ittyspam@yahoo.com>
Re: CUPS Spool File read by perl <spamtrap@dot-app.org>
Re: How to convert MS Word document to text file? <Petri_member@newsguy.com>
Re: How to convert MS Word document to text file? <usenet@morrow.me.uk>
Re: How to convert MS Word document to text file? <flavell@ph.gla.ac.uk>
instance of class into sub? <98734@mail.muni.cz>
Re: instance of class into sub? <ittyspam@yahoo.com>
Re: instance of class into sub? <noreply@gunnar.cc>
Need help with column printing <Lynn.Watts@ugs.com>
Re: Need help with column printing (Anno Siegel)
Re: Need help with column printing <thundergnat@hotmail.com>
Re: parsing a text file <josef.moellers@fujitsu-siemens.com>
Problem with DOS paths in perl - cmd.exe vs. command.ex (Sol Linderstein)
Re: Problem with DOS paths in perl - cmd.exe vs. comman <andre.wisniewski@gmx.de>
random underslashes and single regex (Bill)
random underslashes and single regex (Bill)
Re: random underslashes and single regex <noreply@gunnar.cc>
Re: random underslashes and single regex <pinyaj@rpi.edu>
Re: random underslashes and single regex <xx087@freenet.carleton.ca>
Re: random underslashes and single regex (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 16 Jun 2004 15:28:55 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: [Qs] re "Abigails Coding Guidelines"
Message-Id: <capovn$2vs9$1@agate.berkeley.edu>
[A complimentary Cc of this posting was sent to
gnari
<gnari@simnet.is>], who wrote in article <cakv2d$l4v$1@news.simnet.is>:
> > What does "reentrant" mean?
>
> a bit of code is said to be reentrant when it can tolerate to be
> entered again before finishing. if a new signal is sent to a program
> while it is still processing a previous one, the signal handler needs
> to be reentrant. example of non-reentrant behaviour is to store
> critical temporary information in globals.
Correct, but moot in case of Perl signal handling. As my testing a
decade ago shows, *any* Perl signal handler is unsafe; not just for
signal-inside-signal-handler, but actually for any signal handling.
Since what is buggy is the C wrapper which invokes the Perl signal
handler, this is not fixable from Perl code.
I do not remember this problem to be ever fixed. (Google for my
messages on voodoo patch on this newsgroup for gory details.) The
only situation when the handler has low probability of crashing the
program is that the signal arrives when Perl is blocked in a system
call; if signal arrives when the interpreter is "active", the
probability of failure is about 3%.
Hope this helps,
Ilya
------------------------------
Date: Wed, 16 Jun 2004 15:46:48 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: [Qs] re "Abigails Coding Guidelines"
Message-Id: <capq17$s36$1@wisteria.csv.warwick.ac.uk>
Quoth Ilya Zakharevich <nospam-abuse@ilyaz.org>:
> [A complimentary Cc of this posting was sent to
> gnari
> <gnari@simnet.is>], who wrote in article <cakv2d$l4v$1@news.simnet.is>:
> > > What does "reentrant" mean?
> >
> > a bit of code is said to be reentrant when it can tolerate to be
> > entered again before finishing. if a new signal is sent to a program
> > while it is still processing a previous one, the signal handler needs
> > to be reentrant. example of non-reentrant behaviour is to store
> > critical temporary information in globals.
>
> Correct, but moot in case of Perl signal handling. As my testing a
> decade ago shows, *any* Perl signal handler is unsafe; not just for
> signal-inside-signal-handler, but actually for any signal handling.
> Since what is buggy is the C wrapper which invokes the Perl signal
> handler, this is not fixable from Perl code.
>
> I do not remember this problem to be ever fixed.
Do 5.8's safe signals not fix this? The relevant bits of code, from
mg.c:
| Signal_t
| Perl_csighandler(int sig)
| {
| /* get hold of context, reinstall handler if necessary,
| fake default action of exit if necessary */
|
| if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
| /* Call the perl level handler now--
| * with risk we may be in malloc() etc. */
| (*PL_sighandlerp)(sig);
| else
| Perl_raise_signal(aTHX_ sig);
| }
|
| void
| Perl_raise_signal(pTHX_ int sig)
| {
| /* Set a flag to say this signal is pending */
| PL_psig_pend[sig]++;
| /* And one to say _a_ signal is pending */
| PL_sig_pending = 1;
| }
Ben
--
Every twenty-four hours about 34k children die from the effects of poverty.
Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
that image, that ghastly, grey-billowing, double-barrelled fall, repeated
twelve times every day. Full of children. [Iain Banks] ben@morrow.me.uk
------------------------------
Date: 16 Jun 2004 15:39:13 GMT
From: "Mr. Chow Wing Siu" <wschow@Comp.HKBU.Edu.HK>
Subject: CUPS Spool File read by perl
Message-Id: <cappj1$10bu$1@net44p.hkbu.edu.hk>
Hi,
I am seeking the perl script to read the /var/spool/cups/c* file
to get the info for job originating host and job owner, etc.
Please help. Thanks in advance.
--
Johnson Chow
------------------------------
Date: Wed, 16 Jun 2004 11:50:49 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: CUPS Spool File read by perl
Message-Id: <20040616114740.Q20623@dishwasher.cs.rpi.edu>
On Wed, 16 Jun 2004, Mr. Chow Wing Siu wrote:
> Hi,
>
> I am seeking the perl script to read the /var/spool/cups/c* file
> to get the info for job originating host and job owner, etc.
>
> Please help. Thanks in advance.
What part do you need help with? What have you done so far? What didn't
work the way you expected it to?
The following documentations are all relevant to your goal. Have you read
them yet? If not, type the following commands in your shell. Once you've
read them, let us know if you don't understand something in them.
perldoc -f open
perldoc -f readline
perldoc perlre
Note that if you are looking for a pre-built script tailor made for your
goals, you have come to the wrong place. For that, you should be posting
a job listing to one of the job newsgroups (this isn't one of them).
Paul Lalli
------------------------------
Date: Wed, 16 Jun 2004 18:01:07 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: CUPS Spool File read by perl
Message-Id: <SKydnRsPqNW4Xk3d4p2dnA@adelphia.com>
Mr. Chow Wing Siu wrote:
> I am seeking the perl script to read the /var/spool/cups/c* file
> to get the info for job originating host and job owner, etc.
Have a look at the Net::CUPS module on CPAN. It looks like a lot of what you
want to do has already been done, although the module docs are pretty thin.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 16 Jun 2004 07:59:53 -0700
From: Petri <Petri_member@newsguy.com>
Subject: Re: How to convert MS Word document to text file?
Message-Id: <capn9901jd4@drn.newsguy.com>
In article <Pine.LNX.4.53.0406161452530.12003@ppepc56.ph.gla.ac.uk>, Alan J.
Flavell says...
> On Wed, 16 Jun 2004, Petri wrote:
>> why not let Word save the documents directly as HTML,
> off-topic for Perl, but I doubt that Word can distinguish
> between real HTML, and a hole in the ground!
Well, you have to adjust your level of expectation accordingly, to the level of
quality supplied by the producer.
Or something like that. :)
I know of the (lack of) quality of Word's HTML-output all too well.
But the OP mentioned himself that he had contemplated using Word's
HTML-conversion, so I assumed he new what he was doing.
That is, if his expected userbase are all using MS IE, then any HTML produced by
MS Word will always work.
Petri
------------------------------
Date: Wed, 16 Jun 2004 15:33:33 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: How to convert MS Word document to text file?
Message-Id: <capp8d$qcr$1@wisteria.csv.warwick.ac.uk>
Quoth "Alan J. Flavell" <flavell@ph.gla.ac.uk>:
> On Wed, 16 Jun 2004, Petri wrote:
>
> > why not let Word save the documents directly as HTML,
>
> off-topic for Perl, but I doubt that Word can distinguish between
> real HTML, and a hole in the ground!
I'm no (HT|SG|X)ML expert, but AFAICS Word2k and later produce valid
XML-including-XHTML. This is a somewhat different beast from real HTML,
of course, but it is a perfectly good well-defined browser-supported
format, unlike the mess previous versions of Word used to produce.
> I've had better results by having Word save as RTF, and using
> third-party tools which convert RTF to web formats, preferably with
> some kind of customisation facilities to "tune" the result.
If you're using Perl and want to manage the conversion yourself, I would
recommend using 'Save as Web Page' and then parsing the resulting XML.
It contains all the information in the original Word doc, and is an
easier format to deal with than RTF.
Ben
--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
en@morrow.me.uk (Kate Rusby)
------------------------------
Date: Wed, 16 Jun 2004 17:02:22 +0100
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: How to convert MS Word document to text file?
Message-Id: <Pine.LNX.4.53.0406161651410.12003@ppepc56.ph.gla.ac.uk>
On Wed, 16 Jun 2004, Ben Morrow wrote:
> If you're using Perl and want to manage the conversion yourself, I would
> recommend using 'Save as Web Page' and then parsing the resulting XML.
> It contains all the information in the original Word doc,
That's part of the problem, since most of the minutiae of a typical
Word document (like fonts and other layout dimensions sized in pt
units, to name just one problem area) have no place in HTML that is
meant to be used on the WWW, while Word documents do not necessarily
contain any logical indications such as correspond to HTML's headings,
<abbr>, list-items, and so on, as opposed to visual effects such as
bigger fonts, italics, indents, bullets, and so on.
But I've overstayed my welcome on this un-Perl issue, so "see you on
comp.infosystems.www.*" if you really wanted to pursue this.
------------------------------
Date: Wed, 16 Jun 2004 17:51:06 GMT
From: gep <98734@mail.muni.cz>
Subject: instance of class into sub?
Message-Id: <HzExL6.459@news.muni.cz>
How can i put cgi instance into sub?
i have main cgi script:
#!/usr/bin/perl -w
use CGI;
use strict;
use makepage;
...
my $cg=new CGI;
...
makepage->printhead($cg);
...
In makepage.pm is:
printhead {
my $cg={};
bless $cg, "CGI";
... #it is nice, e.g. $cg->url is OK, but whenever i try $cg->param,
it is empty
}
Don't anybody know, what to do whit this, many thanx, J. Slaby.
------------------------------
Date: Wed, 16 Jun 2004 14:09:53 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: instance of class into sub?
Message-Id: <20040616140435.R20623@dishwasher.cs.rpi.edu>
On Wed, 16 Jun 2004, gep wrote:
> How can i put cgi instance into sub?
> i have main cgi script:
> #!/usr/bin/perl -w
> use CGI;
> use strict;
> use makepage;
> ...
> my $cg=new CGI;
> ...
> makepage->printhead($cg);
> ...
>
>
> In makepage.pm is:
> printhead {
> my $cg={};
> bless $cg, "CGI";
> ... #it is nice, e.g. $cg->url is OK, but whenever i try $cg->param,
> it is empty
> }
>
> Don't anybody know, what to do whit this, many thanx, J. Slaby.
>
You need to read or re-read perldoc perlsub.
First, you didn't declare the subroutine correctly, in that you forgot the
keyword sub. From your statement that "it is nice", I have to assume you
have actual working code somewhere. Please read the posting guidelines
that are posted to this group once a week. They will tell you to post
complete but short scripts that illustrate your problem, and to copy and
paste your code, rather than retype it. That being said, let's move on.
Within your subroutine, you are declaring a new lexical variable $cg. You
are then making this new lexical variable a CGI object. This object has
absolutely nothing to do with the $cg that you passed into the subroutine.
What you want to do is access the variable that you passed in. This is
known as a subroutine argument. Subroutine arguments are passed in via
the @_ array. So within your function:
sub printhead {
my $newcg = $_[0];
my $text = $newcg->param('textfield'); #for example
#...
}
You could call this $newcg any name you want, including $cg. But it
doesn't have anything to do with the original $cg unless you assign it
back to the variable you passed in, as I did by assigning to $_[0].
Again, please make sure you read
perldoc perlsub
to understand more about how subroutine arguments work.
Hope this helps,
Paul Lalli
------------------------------
Date: Wed, 16 Jun 2004 22:30:46 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: instance of class into sub?
Message-Id: <2jboviFv8e0nU2@uni-berlin.de>
Paul Lalli wrote in comp.lang.perl.misc:
> Please read the posting guidelines that are posted to this group
> once a week. They will tell you to post complete but short scripts
> that illustrate your problem, and to copy and paste your code,
> rather than retype it.
And to not multi-post.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 16 Jun 2004 11:57:13 -0700
From: "Lynn" <Lynn.Watts@ugs.com>
Subject: Need help with column printing
Message-Id: <40d09a5a$1@usenet.ugs.com>
Hi All,
I am having problems generating a report (Columns) from data
that is in the form of associative arrays of associative arrays.
I have provided an example of what I am trying to do. I the problem
is I am unable to align the percent column. Any advise as
how to fix this?
Thanks
Lynn
#!/app/bin/perl
use warnings;
use strict;
use diagnostics;
use Text::Table;
my %people = (
'hr' => {
'TOTAL' => 15,
'WIN' => 11,
'HP' => 4,
'BLOG' => 2
},
'jg' => {
'TOTAL' => 14,
'WIN' => 5,
'SGI' => 9,
'BLOG' => 4
},
'my' => {
'TOTAL' => 10,
'WIN' => 9,
'BLOG' => 1,
'HP' => 1
},
'gh' => {
'TOTAL' => 13,
'WIN' => 8,
'SUN' => 5,
'BLOG' => 1
}
);
use vars qw(@by_family $TOTAL $BLOG $HP $IBM $SGI $SUN $WIN $OTH);
my @stuff = qw(TOTAL BLOG HP IBM SGI SUN WIN OTH);
while ( my ( $item, $record ) = each %people ) {
$TOTAL += $record->{TOTAL} unless !( defined( $record->{TOTAL} ) );
$OTH += $record->{OTH} unless !( defined( $record->{OTH} ) );
$BLOG += $record->{BLOG} unless !( defined( $record->{BLOG} ) );
$HP += $record->{HP} unless !( defined( $record->{HP} ) );
$IBM += $record->{IBM} unless !( defined( $record->{IBM} ) );
$SGI += $record->{SGI} unless !( defined( $record->{SGI} ) );
$SUN += $record->{SUN} unless !( defined( $record->{SUN} ) );
$WIN += $record->{WIN} unless !( defined( $record->{WIN} ) );
}
push ( @by_family, $TOTAL, $BLOG, $HP, $IBM, $SGI, $SUN, $WIN, $OTH );
my @percent = ();
push ( @percent, sprintf( '%2d', ( 100 * $by_family[2] / $by_family[0] ) ) )
unless !( defined( $by_family[2] ) or '0' );
push ( @percent, sprintf( '%2d', ( 100 * $by_family[3] / $by_family[0] ) ) )
unless !( defined( $by_family[3] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[4] / $by_family[0] ) ) )
unless !( defined( $by_family[4] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[5] / $by_family[0] ) ) )
unless !( defined( $by_family[5] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[6] / $by_family[0] ) ) )
unless !( defined( $by_family[6] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[7] / $by_family[0] ) ) )
unless !( defined( $by_family[7] ) or '0');
my $tb = Text::Table->new( \"|", 'Name', \"|", @stuff );
$tb->add( $_, @{ $people{$_} }{@stuff} ) for sort keys
%people;
$tb->add( 'TOTAL:', @by_family );
$tb->add( 'PERCENT TOTAL:','N/A', 'N/A', @percent );
print $tb;
------------------------------
Date: 16 Jun 2004 21:20:18 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Need help with column printing
Message-Id: <caqdii$mcb$1@mamenchi.zrz.TU-Berlin.DE>
Lynn <Lynn.Watts@ugs.com> wrote in comp.lang.perl.misc:
> Hi All,
>
> I am having problems generating a report (Columns) from data
> that is in the form of associative arrays of associative arrays.
>
> I have provided an example of what I am trying to do. I the problem
> is I am unable to align the percent column. Any advise as
> how to fix this?
First off, thanks for supplying runnable code that shows the problem.
That makes my job a lot easier. (This is "my job" because I happen to
be the author of Text::Table.)
The alignment is messed up where you build the @percent array. See my
comment in the code.
> #!/app/bin/perl
> use warnings;
> use strict;
> use diagnostics;
> use Text::Table;
>
> my %people = (
> 'hr' => {
> 'TOTAL' => 15,
> 'WIN' => 11,
> 'HP' => 4,
> 'BLOG' => 2
> },
> 'jg' => {
> 'TOTAL' => 14,
> 'WIN' => 5,
> 'SGI' => 9,
> 'BLOG' => 4
> },
> 'my' => {
> 'TOTAL' => 10,
> 'WIN' => 9,
> 'BLOG' => 1,
> 'HP' => 1
> },
> 'gh' => {
> 'TOTAL' => 13,
> 'WIN' => 8,
> 'SUN' => 5,
> 'BLOG' => 1
> }
> );
> use vars qw(@by_family $TOTAL $BLOG $HP $IBM $SGI $SUN $WIN $OTH);
Why are you using package variables here? From the posted code, there
is no reason why these couldn't be lexicals like other variables.
> my @stuff = qw(TOTAL BLOG HP IBM SGI SUN WIN OTH);
Putting the keys into a sequence is a good idea, but you're not making
good use of the list.
> while ( my ( $item, $record ) = each %people ) {
> $TOTAL += $record->{TOTAL} unless !( defined( $record->{TOTAL} ) );
> $OTH += $record->{OTH} unless !( defined( $record->{OTH} ) );
> $BLOG += $record->{BLOG} unless !( defined( $record->{BLOG} ) );
> $HP += $record->{HP} unless !( defined( $record->{HP} ) );
> $IBM += $record->{IBM} unless !( defined( $record->{IBM} ) );
> $SGI += $record->{SGI} unless !( defined( $record->{SGI} ) );
> $SUN += $record->{SUN} unless !( defined( $record->{SUN} ) );
> $WIN += $record->{WIN} unless !( defined( $record->{WIN} ) );
> }
If you *want* undef to be treated as zero, that's a good time to say
no warnings 'uninitialized';
and let Perl handle it.
> push ( @by_family, $TOTAL, $BLOG, $HP, $IBM, $SGI, $SUN, $WIN, $OTH );
Why the push()? @by_family is empty, so an assignment would be clearer.
@by_family = ( $TOTAL, $BLOG, $HP, $IBM, $SGI, $SUN, $WIN, $OTH );
> my @percent = ();
>
> push ( @percent, sprintf( '%2d', ( 100 * $by_family[2] / $by_family[0] ) ) )
> unless !( defined( $by_family[2] ) or '0' );
"unless !" is better written as "if". Also, "or '0'" does nothing to
the boolean value of the expression. The whole condition could be written
if defined $by_family[ 2];
...but it is wrong anyway. If a column isn't defined, you should push
a 0, or a "", or a "-", or anything onto the array. Otherwise the *next*
column will slip into its place. That's what messes up the alignment.
> push ( @percent, sprintf( '%2d', ( 100 * $by_family[3] / $by_family[0] ) ) )
> unless !( defined( $by_family[3] ) or '0');
> push ( @percent, sprintf( '%2d', ( 100 * $by_family[4] / $by_family[0] ) ) )
> unless !( defined( $by_family[4] ) or '0');
> push ( @percent, sprintf( '%2d', ( 100 * $by_family[5] / $by_family[0] ) ) )
> unless !( defined( $by_family[5] ) or '0');
> push ( @percent, sprintf( '%2d', ( 100 * $by_family[6] / $by_family[0] ) ) )
> unless !( defined( $by_family[6] ) or '0');
> push ( @percent, sprintf( '%2d', ( 100 * $by_family[7] / $by_family[0] ) ) )
> unless !( defined( $by_family[7] ) or '0');
> my $tb = Text::Table->new( \"|", 'Name', \"|", @stuff );
> $tb->add( $_, @{ $people{$_} }{@stuff} ) for sort keys
> %people;
> $tb->add( 'TOTAL:', @by_family );
> $tb->add( 'PERCENT TOTAL:','N/A', 'N/A', @percent );
> print $tb;
You have made it unnecessary hard to get the alignment right. For the
"TOTAL" and "PERCENT TOTAL" lines you are using arrays, whiled you use
a hash for the fundamental data structure.
Build the "total" and "percent" structures after the model of your basic
structure, as hashes keyed on @stuff. Then it will be much easier to
keep things aligned. After the setup of %people, this is how I would
continue:
my @stuff = qw(TOTAL BLOG HP IBM SGI SUN WIN OTH);
my %by_family;
while ( my ( $item, $record) = each %people ) {
no warnings 'uninitialized';
$by_family{ $_} += $record->{ $_} for @stuff;
}
my %percent;
$percent{ $_} = 100 * $by_family{ $_} / $by_family{ TOTAL} for @stuff;
$_ = sprintf '%.0f', $_ for values %percent;
my $tb = Text::Table->new( \"|", 'Name', \"|", @stuff );
$tb->add( $_, @{ $people{$_} }{@stuff} ) for sort keys %people;
$tb->add( 'TOTAL:', @by_family{ @stuff} );
$tb->add( 'PERCENT TOTAL:', 'N/A', 'N/A', @percent{ @stuff[ 2 .. $#stuff]});
print $tb;
The percent data is calculated for two cases that aren't needed (TOTAL and
BLOG). It would have been more difficult to exclude them. Instead, we
exclude them using only @stuff[ 2 .. $#stuff]. Add two 'N/A's, skip
two items in @stuff... the columns clearly stay aligned.
Anno
------------------------------
Date: Wed, 16 Jun 2004 17:21:31 -0400
From: thundergnat <thundergnat@hotmail.com>
Subject: Re: Need help with column printing
Message-Id: <40d0b9d3$0$3021$61fed72c@news.rcn.com>
Lynn wrote:
> Hi All,
>
> I am having problems generating a report (Columns) from data
> that is in the form of associative arrays of associative arrays.
>
> I have provided an example of what I am trying to do. I the problem
> is I am unable to align the percent column. Any advise as
> how to fix this?
>
> Thanks
>
> Lynn
>
The misalignment is due to the different handling of numbers and
strings. Your counts are all numbers but your percentages are strings.
If you make your percentages numbers too, they all line up.
Read the doc for Text::Aligner on which Text::Table is based.
Your data structures cry out for hashes or arrays too.
#!/app/bin/perl
use warnings;
use strict;
use diagnostics;
use Text::Table;
my %people = (
'hr' => {
'TOTAL' => 15,
'WIN' => 11,
'HP' => 4,
'BLOG' => 2
},
'jg' => {
'TOTAL' => 14,
'WIN' => 5,
'SGI' => 9,
'BLOG' => 4
},
'my' => {
'TOTAL' => 10,
'WIN' => 9,
'BLOG' => 1,
'HP' => 1
},
'gh' => {
'TOTAL' => 13,
'WIN' => 8,
'SUN' => 5,
'BLOG' => 1
}
);
my %counts;
my @percent = ();
my @totals = ();
my @stuff = qw(TOTAL BLOG HP IBM SGI SUN WIN OTH);
while ( my ( $item, $record ) = each %people ) {
for (@stuff){
$counts{$_} = 0 unless $counts{$_};
$counts{$_} += $record->{$_} if $record->{$_}
}
}
for ('HP','IBM','SGI','SUN','WIN','OTH'){
push ( @percent, int( 100 * $counts{$_} / $counts{TOTAL} ) );
}
for (@stuff){
push ( @totals, $counts{$_} );
}
my $tb = Text::Table->new( \"|", 'Name', \"|", @stuff );
$tb->add( $_, @{ $people{$_} }{@stuff} ) for sort keys
%people;
$tb->add( 'TOTAL:', @totals );
$tb->add( 'PERCENT TOTAL:', 'N/A', 'N/A', @percent );
print $tb;
------------------------------
Date: Wed, 16 Jun 2004 17:14:44 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: parsing a text file
Message-Id: <capns9$5q9$1@nntp.fujitsu-siemens.com>
Paul Lalli wrote:
> On Wed, 16 Jun 2004, Josef Moellers wrote:
>> if ($state eq 0) {
>> } elsif ($state eq 1) {
>> } elsif ($state eq 2) {
>> } elsif ($state eq 3) {
>> if ($state eq 3) {
> Please don't confuse numeric and string contexts. Yes, in this small
> example, everything you did will 'work', but you're asking for trouble.=
> If you're assigning integers, you should be comparing integers. If you=
're
> assigning strings, you should be comparing strings. In other words, yo=
u
> need to change EITHER all instances of
> $state =3D 0;
> to
> $state =3D '0';
>=20
> or (much preferred in this case) all instances of
> ($state eq 0)
> to
> ($state =3D=3D 0)
Yes, you're right. They should be numbers (but anything would do, even=20
symbolic states like "Initial", "Title match", "Version match",=20
"Printing" rather than 0, 1, 2, 3!) and it should be =3D=3D rather than e=
q.
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
------------------------------
Date: 16 Jun 2004 10:54:44 -0700
From: slinderstein@yahoo.com (Sol Linderstein)
Subject: Problem with DOS paths in perl - cmd.exe vs. command.exe
Message-Id: <e435b329.0406160954.79bc9d94@posting.google.com>
Hi,
I've got the following two line perl program that I run on a Windows
XP Pro system:
print `c:\\windows\\system32\\cmd.exe /c dir c:\\documents and
settings\\me\\Desktop` ;
print `dir c:\\docume~1\\me\\desktop` ;
When I run it the first line produces an error: The system cannot find
the file specified. The second line produces the desired output.
I'm confused because cmd.exe supports spaces in directory paths yet
it's not supporting them in this perl program.
Any idea on how I can get perl to do the right thing with backticks
and paths in DOS?
Thanks,
Sol
------------------------------
Date: Wed, 16 Jun 2004 20:21:46 +0200
From: "Andre Wisniewski" <andre.wisniewski@gmx.de>
Subject: Re: Problem with DOS paths in perl - cmd.exe vs. command.exe
Message-Id: <opr9o9akw0udlqzq@ares>
On 16 Jun 2004 10:54:44 -0700, Sol Linderstein <slinderstein@yahoo.com>
wrote:
> Hi,
>
> I've got the following two line perl program that I run on a Windows
> XP Pro system:
>
> print `c:\\windows\\system32\\cmd.exe /c dir c:\\documents and
> settings\\me\\Desktop` ;
>
> print `dir c:\\docume~1\\me\\desktop` ;
>
> When I run it the first line produces an error: The system cannot find
> the file specified. The second line produces the desired output.
>
> I'm confused because cmd.exe supports spaces in directory paths yet
> it's not supporting them in this perl program.
>
> Any idea on how I can get perl to do the right thing with backticks
> and paths in DOS?
>
> Thanks,
> Sol
The spaces are the problem nevertheless. Try it with:
print `c:\\windows\\system32\\cmd.exe /c dir "c:\\dokumente und
einstellungen\\me\\desktop"`;
^^^ german xp
;-) ^^^
--
Andre
---
Fighting for peace is like f..ing for virginity
------------------------------
Date: 16 Jun 2004 13:13:10 -0700
From: wherrera@lynxview.com (Bill)
Subject: random underslashes and single regex
Message-Id: <239ce42f.0406160748.30720528@posting.google.com>
Anyone know of a _single regex_ for the following:
Match a string which has a single '_' character placed at random
within it. The string without the '_' is known, the position of the
'_' is not known.
I know one can strip out the '_' and then match, but I'd like to do it
with one m// statement.
Thanks.
------------------------------
Date: 16 Jun 2004 13:19:41 -0700
From: wherrera@lynxview.com (Bill)
Subject: random underslashes and single regex
Message-Id: <239ce42f.0406160747.287ce85a@posting.google.com>
Anyone know of a _single regex_ for the following:
Match a string which has a single '_' character placed at random
within it. The string without the '_' is known, the position of the
'_' is not known.
I know one can strip out the '_' and then match, but I'd like to do it
with one m// statement.
Thanks.
------------------------------
Date: Wed, 16 Jun 2004 22:26:08 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: random underslashes and single regex
Message-Id: <2jbomsFv8e0nU1@uni-berlin.de>
Bill wrote:
> Anyone know of a _single regex_ for the following:
>
> Match a string which has a single '_' character placed at random
> within it. The string without the '_' is known, the position of the
> '_' is not known.
>
> I know one can strip out the '_' and then match, but I'd like to do
> it with one m// statement.
Why?? Please explain.
Without having seen the regex, I suppose that character classes is the
key.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 16 Jun 2004 16:57:01 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: random underslashes and single regex
Message-Id: <Pine.SGI.3.96.1040616165620.317301A-100000@vcmr-64.server.rpi.edu>
On 16 Jun 2004, Bill wrote:
>Anyone know of a _single regex_ for the following:
>
>Match a string which has a single '_' character placed at random
>within it. The string without the '_' is known, the position of the
>'_' is not known.
Do you mean you want to match strings that contain ONLY one underscore in
them?
if (($str =~ tr/_//) == 1) { it's ok }
or
if ($str =~ /\A[^_]*_[^_]*\z/) { it's ok }
--
Jeff Pinyan RPI Acacia Brother #734 RPI Acacia Corp Secretary
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
------------------------------
Date: 16 Jun 2004 20:51:27 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: random underslashes and single regex
Message-Id: <slrncd1cmf.l0.xx087@smeagol.ncf.ca>
Bill <wherrera@lynxview.com> wrote:
> Anyone know of a _single regex_ for the following:
>
> Match a string which has a single '_' character placed at random
> within it. The string without the '_' is known, the position of the
> '_' is not known.
>
> I know one can strip out the '_' and then match, but I'd like to do it
> with one m// statement.
my $regex = join('_?', split('', $known_string));
if ($some_text =~ /$regex/) {
# some text contains the known string possibly with underscore(s)
}
--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca
------------------------------
Date: 16 Jun 2004 22:02:15 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: random underslashes and single regex
Message-Id: <caqg17$ntn$1@mamenchi.zrz.TU-Berlin.DE>
Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote in comp.lang.perl.misc:
> On 16 Jun 2004, Bill wrote:
>
> >Anyone know of a _single regex_ for the following:
> >
> >Match a string which has a single '_' character placed at random
> >within it. The string without the '_' is known, the position of the
> >'_' is not known.
>
> Do you mean you want to match strings that contain ONLY one underscore in
> them?
>
> if (($str =~ tr/_//) == 1) { it's ok }
>
> or
>
> if ($str =~ /\A[^_]*_[^_]*\z/) { it's ok }
He wants, for a given string, a regex that matches exactly the strings
that are obtained by inserting a single '_' into the string.
The obvious solution is to build the set of strings and or them
together:
@re = map $substr( $str, 0, $_) . '_' . substr( $str, $_),
1 .. -1 + length $str;
$re = join '|', @re;
There must be more interesting ways to generate the list @re.
$str = "_$str";
@re = map { $str =~ s/_(.)/${1}_/; $str } 1 .. -2 + length $str;
Anno
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6698
***************************************