[25925] in Perl-Users-Digest
Perl-Users Digest, Issue: 8147 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 3 21:05:31 2005
Date: Fri, 3 Jun 2005 18:05:04 -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 Fri, 3 Jun 2005 Volume: 10 Number: 8147
Today's topics:
Re: counting word occurances <someone@example.com>
Re: counting word occurances <1usa@llenroc.ude.invalid>
Re: counting word occurances <tadmc@augustmail.com>
Generating formetted documents in Perl <russeld@pobox.com>
Re: Generating formetted documents in Perl <tadmc@augustmail.com>
Passing a $1 within a scalar to a s/// doesn't work. Wh <news@robertcampbellFERRET.co.uk>
Re: Passing a $1 within a scalar to a s/// doesn't work <noreply@gunnar.cc>
Re: Passing a $1 within a scalar to a s/// doesn't work <jgibson@mail.arc.nasa.gov>
Re: Passing a $1 within a scalar to a s/// doesn't work <ng7@robertcampbell.co.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 03 Jun 2005 20:19:51 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: counting word occurances
Message-Id: <HX2oe.31147$on1.29725@clgrps13>
A. Sinan Unur wrote:
> vali <vticau@excite.com> wrote in
> news:r80oe.6452$II3.3529@news.cpqcorp.net:
>
>>Jürgen Exner wrote:
>
>>>See
>>> perldoc -q "strip blank"
>>>
>>>Another difference between our solutions would be the handling of
>>>lines that contain more than one single word, e.g. "green grapes" or
>>>"mini-tomatos". Which behaviour the OP wants is everybody's guess.
>
>>Wasn't aware about the above faq. I've been using for years:
>>s/(^\s+|\s+$)//g;
>>which seems to be the same (or not ?!) as:
>>s/^\s*//; s/\s*$//;
>
> Not functionally the same. Your expression requires at least one \s either
> at the beginning or the end.
>
> Second, it uses alternation in the regex which is generally more expensive.
>
> Third, you are unnecessarily capturing.
>
> use strict;
> use warnings;
> #! /usr/bin/perl
>
> use strict;
> use warnings;
>
> use Benchmark ':all';
>
> my $INPUT = [
> 'pear ',
> ' apple ',
> 'apple',
> ' orange ',
> ' mango ',
> 'mango',
> ' pear',
> ' cherry ',
> 'apple',
> '',
> ];
>
> sub faq {
> my @input = @{ $INPUT };
> for (@input) {
> s/^\s*//;
> s/\s*$//;
> }
> }
The FAQ uses \s+ instead of \s* which is more efficient.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 3 Jun 2005 20:36:35 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: counting word occurances
Message-Id: <Xns966AA9E35FBD1asu1cornelledu@132.236.56.8>
"John W. Krahn" <someone@example.com> wrote in
news:HX2oe.31147$on1.29725@clgrps13:
> A. Sinan Unur wrote:
>> vali <vticau@excite.com> wrote in
>> news:r80oe.6452$II3.3529@news.cpqcorp.net:
...
>>>Wasn't aware about the above faq. I've been using for years:
>>>s/(^\s+|\s+$)//g;
>>>which seems to be the same (or not ?!) as:
>>>s/^\s*//; s/\s*$//;
...
>> sub faq {
>> my @input = @{ $INPUT };
>> for (@input) {
>> s/^\s*//;
>> s/\s*$//;
>> }
>> }
>
> The FAQ uses \s+ instead of \s* which is more efficient.
Thank you for the correction. I failed to notice the typo in vali's post.
I was suprised to see just how much more efficient \s+ was compared to \s*.
Finally, this supports my assertion that s/(^\s+|\s+$)//g; is not the same
as what the answer to the FAQ recommends.
#! /usr/bin/perl
use strict;
use warnings;
use Benchmark ':all';
my $INPUT = [
'pear ',
' apple ',
'apple',
' orange ',
' mango ',
'mango',
' pear',
' cherry ',
'apple',
'',
];
sub s1 {
my @input = @{ $INPUT };
for (@input) {
s/^\s*//;
s/\s*$//;
}
}
sub faq {
my @input = @{ $INPUT };
for (@input) {
s/^\s+//;
s/\s+$//;
}
}
cmpthese 0, {
s1 => \&s1,
faq => \&faq,
};
__END__
D:\Home>perl t.pl
Rate s1 faq
s1 8638/s -- -25%
faq 11489/s 33% --
Sinan
------------------------------
Date: Fri, 3 Jun 2005 17:16:48 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: counting word occurances
Message-Id: <slrnda1lmg.6hq.tadmc@magna.augustmail.com>
A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
> I was suprised to see just how much more efficient \s+ was compared to \s*.
That shouldn't be too surprising after applying some intuition.
Patterns with required elements describe a smaller set of matching
strings, and they allow the regex engine to "fail early" as soon
as it is determined that the required thing is not where it is
required to be.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 03 Jun 2005 15:48:52 -0500
From: Russel Dalenberg <russeld@pobox.com>
Subject: Generating formetted documents in Perl
Message-Id: <gY6dnZpigYsxXz3fRVn-hQ@io.com>
I'm trying to write a Perl program that will output a simple catalog. I'd
like to produce something nicer than raw text output.
I have entries with three fields, and I want to be able to display the
fields in columns. I'd like to provide headers and footers with page
numbers (in alternating corners for even/odd pages, if possible). It would
also be really nice if I could include a horizontal line between some of
the entries. I don't need much in the way of fonts, just a couple of
sizes, and maybe bold and italic.
I want the program to be portable between systems (Windows and Linux
presently, Mac OSX eventually), and the output format as well. Ideally,
I'd like to create an output file that the user can bring to his local
print shop to get printed.
I looked through CPAN some, and found RTF::Writer and PDF::Create, but
neither seems to be easy to use, or able to give me the type of output I'm
looking for. This seems strange since I think the page format I'm trying
to produce is pretty simple.
Am I just missing something with the RTF and PDF packages, or is there some
other way that I should use to generate formatted output. I'm about ready
to say "heck with it" and see if I can generate troff files. :-)
Any pointers to documentation that might help, or examples that help me
figure out how to do the formatting would be greatly appreciated!
--
Russel Dalenberg
russeld@pobox.com
------------------------------
Date: Fri, 3 Jun 2005 17:22:26 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Generating formetted documents in Perl
Message-Id: <slrnda1m12.6hq.tadmc@magna.augustmail.com>
Russel Dalenberg <russeld@pobox.com> wrote:
> I'm trying to write a Perl program that will output a simple catalog. I'd
> like to produce something nicer than raw text output.
> fields in columns. I'd like to provide headers and footers with page
> numbers (in alternating corners for even/odd pages, if possible).
> or is there some
> other way that I should use to generate formatted output.
I'd generate *structured* output from your Perl (or other) program,
and have some other program format that for me.
> and see if I can generate troff files. :-)
I'd generate XML, perhaps in a catalog-specific schema or even
in DocBook, and let one of the bazillion XML processors turn it
into PDF, or troff or HTML or...
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 03 Jun 2005 19:18:25 +0100
From: Rob Campbell <news@robertcampbellFERRET.co.uk>
Subject: Passing a $1 within a scalar to a s/// doesn't work. Why?
Message-Id: <d7q6ui$7mr$1@news.ox.ac.uk>
Hi,
Still in the early stages of this Perl business but haven't been able to
find an answer to this question anywhere. Perhaps someone here can help.
I have a little function which takes as its inputs two regular expressions.
The first line of the function is:
my($simulate,$oldpart,$newpart,@rest) = @ARGV;
The function just uses these to perform a substitution:
$d =~ s/$oldpart/$newpart/;
The problem is that when I want to capture text in the first expression to
use it as a "$1" in the second, it don't work: the '$1' is interpretted
literally.
i.e.
if $oldpart contains 'binoise\w*(G1_[0-9]+)\.src', and and $newpart contains
'_binoise.src'; then I get, for example:
./binoise9G1_12.src -> ./_binoise.src
Not what I want, but fine, it works. However, if $newpart now contains
'$1_binoise.src' then I get:
./binoise9G1_12.src -> ./$1_binoise.src
Huh? Why is the $1 being interpreted literally. I've not done anything else
silly since if I hard-code my substitution so that it looks like this:
$d =~ s/$oldpart/$1_binoise/;
Then I get what I want:
./binoise9G1_12.src -> ./G1_12_binoise
I've tried escaping the $ in $newpart but that doesn't work either. Why does
Perl interpret my substitution command differently when I feed it a
variable compared to when I feed it the regexp directly? How do I get it to
play ball and allow a $1 to be interpretted correctly when it's passed to
my s/// command in a scalar.
Thanks!
Rob
--
remove "FERRET" to reply
------------------------------
Date: Fri, 03 Jun 2005 21:51:08 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Passing a $1 within a scalar to a s/// doesn't work. Why?
Message-Id: <3gbqvlFbli8pU1@individual.net>
Rob Campbell wrote:
> How do I get it to play ball and allow a $1 to be interpretted
> correctly when it's passed to my s/// command in a scalar.
perldoc -q "expand variables"
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 03 Jun 2005 12:59:23 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Passing a $1 within a scalar to a s/// doesn't work. Why?
Message-Id: <030620051259237291%jgibson@mail.arc.nasa.gov>
In article <d7q6ui$7mr$1@news.ox.ac.uk>, Rob Campbell
<news@robertcampbellFERRET.co.uk> wrote:
> Hi,
>
> Still in the early stages of this Perl business but haven't been able to
> find an answer to this question anywhere. Perhaps someone here can help.
>
> I have a little function which takes as its inputs two regular expressions.
> The first line of the function is:
> my($simulate,$oldpart,$newpart,@rest) = @ARGV;
>
> The function just uses these to perform a substitution:
> $d =~ s/$oldpart/$newpart/;
>
> The problem is that when I want to capture text in the first expression to
> use it as a "$1" in the second, it don't work: the '$1' is interpretted
> literally.
>
> i.e.
> if $oldpart contains 'binoise\w*(G1_[0-9]+)\.src', and and $newpart contains
> '_binoise.src'; then I get, for example:
> ./binoise9G1_12.src -> ./_binoise.src
>
> Not what I want, but fine, it works. However, if $newpart now contains
> '$1_binoise.src' then I get:
> ./binoise9G1_12.src -> ./$1_binoise.src
>
> Huh? Why is the $1 being interpreted literally. I've not done anything else
> silly since if I hard-code my substitution so that it looks like this:
> $d =~ s/$oldpart/$1_binoise/;
> Then I get what I want:
> ./binoise9G1_12.src -> ./G1_12_binoise
>
>
> I've tried escaping the $ in $newpart but that doesn't work either. Why does
> Perl interpret my substitution command differently when I feed it a
> variable compared to when I feed it the regexp directly? How do I get it to
> play ball and allow a $1 to be interpretted correctly when it's passed to
> my s/// command in a scalar.
It helps if you post a complete program anybody can run.
I am not quite sure why '$d =~ /$oldpart/$newpart/e;' doesn't work, but
wrapping the replacement text in double quotes and forcing another
round of evaluation works:
#!/usr/local/bin/perl
use strict;
use warnings;
f('./binoise9G1_12.src', 'binoise\w*(G1_[0-9]+)\.src',
'$1_binoise.src' );
sub f
{
my( $text, $oldpart, $newpart ) = @_;
print "before: <$text>\n";
$newpart = "\"$newpart\"";
$text =~ s/$oldpart/$newpart/ee;
print "after: <$text>\n";
}
__END__
Jim 67% perl campbell.pl
before: <./binoise9G1_12.src>
after: <./G1_12_binoise.src>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
------------------------------
Date: Fri, 03 Jun 2005 20:23:18 GMT
From: Rob Campbell <ng7@robertcampbell.co.uk>
Subject: Re: Passing a $1 within a scalar to a s/// doesn't work. Why?
Message-Id: <W_2oe.6484$iy2.2223@newsfe1-gui.ntli.net>
Cheers!
This is what I needed. Thanks very much!
> $newpart = "\"$newpart\"";
> $text =~ s/$oldpart/$newpart/ee;
------------------------------
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 8147
***************************************