[22693] in Perl-Users-Digest
Perl-Users Digest, Issue: 4914 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 30 03:11:29 2003
Date: Wed, 30 Apr 2003 00:10:10 -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, 30 Apr 2003 Volume: 10 Number: 4914
Today's topics:
Re: Reference Question <uri@stemsystems.com>
Re: Reference Question <mgjv@tradingpost.com.au>
Re: Reference Question <uri@stemsystems.com>
Re: regex for word whitespace word <johngros@bigpond.net.au>
Re: Rounding the figures..... (Tad McClellan)
Re: search, replace and insert. <krader@skepticism.us>
Re: Splitting string in array by 2 characters <bart.lateur@pandora.be>
Subtracting strings [long post] (Quantum Mechanic)
Re: Subtracting strings [long post] (Jay Tilton)
Re: Test::Harness annoyance <jkeen@concentric.net>
Why is POE idle most of the time? (jtd)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 30 Apr 2003 03:40:10 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Reference Question
Message-Id: <x7isswzm91.fsf@mail.sysarch.com>
>>>>> "WK" == Winfried Koenig <w.koenig@acm.org> writes:
WK> Uri Guttman wrote:
>>>>>>> "MS" == Michael Spertus <mps@geodesic.com> writes:
MS> How do I create a reference to a list returned by subroutine.
>> you can't get a reference to a list. you either return a list ref
>> from
>> the sub (if you coded that sub) or copy the list to a new anon array and
>> use that:
>> @array = [ sub_returning_list() ] ;
WK> a typo, better use:
WK> $array_ref = [ sub_returning_list() ];
not a typo.
How do I create a reference to a list returned by subroutine. In
other words, I have a subroutine "foo" that I want to invoke in
list context and store a reference to the returned list. None of
^^^^^^^^^^^^
the obvious things seem to work.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 30 Apr 2003 06:24:27 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Reference Question
Message-Id: <slrnbaur0r.3gj.mgjv@verbruggen.comdyn.com.au>
On Wed, 30 Apr 2003 03:40:10 GMT,
Uri Guttman <uri@stemsystems.com> wrote:
>>>>>> "WK" == Winfried Koenig <w.koenig@acm.org> writes:
>
> WK> Uri Guttman wrote:
> >>>>>>> "MS" == Michael Spertus <mps@geodesic.com> writes:
> MS> How do I create a reference to a list returned by subroutine.
> >> you can't get a reference to a list. you either return a list ref
> >> from
> >> the sub (if you coded that sub) or copy the list to a new anon array and
> >> use that:
> >> @array = [ sub_returning_list() ] ;
>
> WK> a typo, better use:
>
> WK> $array_ref = [ sub_returning_list() ];
>
> not a typo.
>
> How do I create a reference to a list returned by subroutine. In
> other words, I have a subroutine "foo" that I want to invoke in
> list context and store a reference to the returned list. None of
> ^^^^^^^^^^^^
> the obvious things seem to work.
But the [] already puts the sub in list context. In both code
fragments above, sub_returning_list is called in list context. The
assignment of the resulting reference has no influence on that. I see
nothing in the above that says that the reference should be stored in
an array element. Besides, if that was wanted, I'd probably do
$array[0] = [ sub_returning_list() ];
or something like that, just to document that I understand that both
left and right of the '=' we have a scalar. Assignment of scalars to
lists to me always look like a potential error, and whenever I write
code that has something like that, I tend to have some documentation
around it explaining that, yes, I did mean to assign a scalar to an
array.
Martien
--
|
Martien Verbruggen | If it isn't broken, it doesn't have enough
Trading Post Australia | features yet.
|
------------------------------
Date: Wed, 30 Apr 2003 06:42:06 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Reference Question
Message-Id: <x73ck0zdtt.fsf@mail.sysarch.com>
>>>>> "MV" == Martien Verbruggen <mgjv@tradingpost.com.au> writes:
>> >> @array = [ sub_returning_list() ] ;
>>
WK> a typo, better use:
>>
WK> $array_ref = [ sub_returning_list() ];
>>
>> not a typo.
>>
>> How do I create a reference to a list returned by subroutine. In
>> other words, I have a subroutine "foo" that I want to invoke in
>> list context and store a reference to the returned list. None of
>> ^^^^^^^^^^^^
>> the obvious things seem to work.
MV> $array[0] = [ sub_returning_list() ];
MV> or something like that, just to document that I understand that both
MV> left and right of the '=' we have a scalar. Assignment of scalars to
MV> lists to me always look like a potential error, and whenever I write
MV> code that has something like that, I tend to have some documentation
MV> around it explaining that, yes, I did mean to assign a scalar to an
MV> array.
we haven't heard back from the OP. there are many ways that could be
called. maybe he did something like:
@array = ( [ sub_returning_list() ], [ sub_returning_list2() ] ) ;
or:
another_sub( [ sub_returning_list() ] ) ;
which also provides list context but he wanted an array ref.
my code was just providing list context as asked for but a reference was
wanted. he never clarified it beyond that. my code was just answering
the ref in list context question and nothing more.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 30 Apr 2003 05:53:41 GMT
From: "John Gros" <johngros@bigpond.net.au>
Subject: Re: regex for word whitespace word
Message-Id: <FxJra.7456$lD4.78915@news-server.bigpond.net.au>
I have looked a HTML::Parse, it seemed pretty confusing for me. Since I
found my errors with the regex, I find it easy to parse the HTML I want any
way I want. Maybe HTML::TokeParse might be simpler, but a good regex is all
I need. I think they are solid enough to avoid being broken by site revamps.
"w i l l" <willis3140@yahoo.com> wrote in message
news:6tatavkuolchmjlusgchmoisou6o1r3r6d@4ax.com...
> HTML::TokeParse might help get you going in the right direction
>
> w i l l
>
------------------------------
Date: Tue, 29 Apr 2003 21:24:42 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Rounding the figures.....
Message-Id: <slrnbaucva.3el.tadmc@magna.augustmail.com>
Sisyphus <kalinabears@hdc.com.au> wrote:
> "Sara" <sara_samsara5@yahoo.com> wrote in message
> news:9365982e.0304291327.1c1255a0@posting.google.com...
>> Is there any way to round the figures
> I think most use sprintf(). (See perldoc -q round.)
^^^^^^^^
> I like to use:
^^^^^^
> my $x_rounded = int($x + 0.5);
Go with "most" then. :-)
---------------------
#!/usr/bin/perl
use strict;
use warnings;
my $x = -12.2;
my $x_rounded = int($x + 0.5);
print "$x rounds to $x_rounded\n";
my $X_rounded = sprintf "%0.f", $x;
printf "%f rounds to %.0f\n", $x, $X_rounded;
---------------------
output:
-12.2 rounds to -11
-12.200000 rounds to -12
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 29 Apr 2003 19:38:17 -0700
From: "Kurtis D. Rader" <krader@skepticism.us>
Subject: Re: search, replace and insert.
Message-Id: <pan.2003.04.30.02.38.11.885589@skepticism.us>
On Thu, 24 Apr 2003 17:34:32 +0900, Abhi wrote:
> but the problem is I also need to do this :
>
> perl -pi -e 's|^#<IfModule mod_proxy.c>|<IfModule mod_proxy.c>|g'
> /etc/httpd/conf/httpd.conf
>
> and insert a few entries on a new line afterwards, and then uncomment the
> *next* matching </IfModule>
I'm surprised no one has suggested slurping and operating on the whole
file. If the input is potentially larger than what you can fit in memory
then this is a bad idea. But an Apache configuration file is all but
guaranteed to be small enough that slurping it all into memory is not going
to be a problem.
perl -0777 -pi -e 'whatever'
The "-pi" mechanism isn't constrained to working line by line.
Your substitutions then simply employ the "m" modifier. You can also split
the file and operate on it line by line while still leveraging the "-i"
magic. Of course, if you do that it's probably better to use "-n" rather
than "-p" and print the array yourself. For example,
perl -0777 -ni -e '@lines = split /\n/; whatever; print @lines;'
------------------------------
Date: Wed, 30 Apr 2003 06:37:20 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Splitting string in array by 2 characters
Message-Id: <8nruav438t9e2nfu809gqqm67pi1rer4io@4ax.com>
kderaedt wrote:
>I was trying with
>@MSISDN = unpack((length($Field[0])/2), $Field[0]);
Doesn't work like that; but you're on to something.
$_ = '0A000200010103020407040906030207020000';
@MSISDN = unpack("a2" x (length($_)/2), $_);
print "@MSISDN\n";
-->
0A 00 02 00 01 01 03 02 04 07 04 09 06 03 02 07 02 00 00
--
Bart.
------------------------------
Date: 29 Apr 2003 20:52:19 -0700
From: quantum_mechanic_1964@yahoo.com (Quantum Mechanic)
Subject: Subtracting strings [long post]
Message-Id: <f233f2f0.0304291952.ccfcad4@posting.google.com>
I'm working on a puzzle, and I'm looking for a simple, clean way to
find the "difference" between 2 strings. The 2nd string must be a
proper substring of the 1st -- if there are characters in the 2nd
string not in the 1st, or there is an excess of one or more characters
in the 2nd string compared to the 1st, a failing result is returned.
For example,
$p = 'baseball';
$q = 'blab';
$r = string_diff( $p, $q ); # $r eq "seal" [in any order]
but
$p = 'baseball';
$q = 'bulb';
$r = string_diff( $p, $q ); # $r == 0 because 'u' not in $p
I'm not really interested in the character order of either the inputs
or the outputs.
Two less-than-satisfactory methods I've come up with:
#### 1: character set hashes ####
# create a hash of char counts based on the given string
sub char_set
{
my %char_set;
map { $char_set{$_}++ } split //, shift;
return \%char_set;
}
# compare 2 string "sets", return the difference
# note that counts of the 2nd are subtracted from the first
# if a negative count occurs, return 0 as a scalar
sub char_set_diff
{
my %one = %{ +shift }; # make a copy ( disambiguate shift with + )
my %two = %{ +shift };
# subtract counts
map { $one{$_} -= $two{$_} } keys %two;
# check for improper subset
my $proper_subset = not grep { $_ < 0 } values %one;
return \%one;
} # char_set_diff
# return a string given a char set
sub char_set_2_string
{
my $char_set = shift;
return join '', map { $_ x ${$char_set}{$_} } keys %{ $char_set };
}
# subtract one string from another
sub string_diff
{
my $one = shift;
my $two = shift;
my $xx = char_set( $one );
my $yy = char_set( $two );
my $diff = char_set_diff( $xx, $yy);
if ( ref $diff )
{
return char_set_2_string( $diff );
}
else
{
return 0; # failed
}
}
my $z = string_diff( 'baseball', 'blab' ); # $z eq "seal" [in some
order]
#### 2: substr and index
sub string_diff
{
my $one = shift;
my $two = shift;
foreach $c ( split //, $two )
{
my $i = index( $one, $c );
if ( $i >= 0 ) # found it
{
substr( $one, $r, 1, '' ); # replace with empty string
}
else
{
return 0; # failed
}
} # foreach char
return $one;
} # string_diff
my $z = string_diff( 'baseball', 'blab' ); # $z eq "seal" [in this
order!]
##########
Anyone have anything better? I've tried searching, but I haven't hit
on discriminating search terms.
TIA,
-QM
------------------------------
Date: Wed, 30 Apr 2003 06:14:59 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Subtracting strings [long post]
Message-Id: <3eaf647b.568817771@news.erols.com>
quantum_mechanic_1964@yahoo.com (Quantum Mechanic) wrote:
: I'm working on a puzzle, and I'm looking for a simple, clean way to
: find the "difference" between 2 strings. The 2nd string must be a
: proper substring of the 1st -- if there are characters in the 2nd
: string not in the 1st, or there is an excess of one or more characters
: in the 2nd string compared to the 1st, a failing result is returned.
That's not the usual interpretation of "substring," but the
explanation is clear enough.
: Two less-than-satisfactory methods I've come up with:
What exactly do you find unsatisfactory about them?
: #### 1: character set hashes ####
: # create a hash of char counts based on the given string
[snip]
: # compare 2 string "sets", return the difference
: # note that counts of the 2nd are subtracted from the first
: # if a negative count occurs, return 0 as a scalar
[snip]
: # return a string given a char set
[snip]
: # subtract one string from another
[snip]
The gist of that algorithm is peachy, but the rendition is spaghetti.
Unless each of those subs is used for other purposes, I'd just hammer
them all into a single sub. In fact, the result is so short that I'd
do that anyway.
sub string_diff {
my %seen;
# Values of %seen will be the difference in character counts
# between the first string and the second.
my @foo = ( undef, @_ ); # so we can use indeces 1 and -1
for my $i (1, -1) {
$seen{$_} += $i for $foo[$i] =~ /./g;
}
# Verify that all letters in the second word
# appear in the first.
$_ < 0 && return 0 for values %seen;
# Smush remaining letters into a return string.
return join '', map $_ x $seen{$_}, keys %seen;
}
: #### 2: substr and index
:
: sub string_diff
: {
: my $one = shift;
: my $two = shift;
:
: foreach $c ( split //, $two )
: {
: my $i = index( $one, $c );
: if ( $i >= 0 ) # found it
: {
: substr( $one, $r, 1, '' ); # replace with empty string
: }
: else
: {
: return 0; # failed
: }
: } # foreach char
:
: return $one;
: } # string_diff
That one's a little verbose, but not shabby at all. For my tastes,
I'd use s/// instead of index/substr.
sub string_diff {
my( $one, $two ) = @_;
$one =~ s/$_// or return 0 for( $two =~ /./g );
return $one;
}
------------------------------
Date: 30 Apr 2003 01:49:04 GMT
From: "James E Keenan" <jkeen@concentric.net>
Subject: Re: Test::Harness annoyance
Message-Id: <b8na2g$r32@dispatch.concentric.net>
"Bart Lateur" <bart.lateur@pandora.be> wrote in message
news:ngjsavksj8ut1gjfke8tm6cug8h8i0ropn@4ax.com...
> James E Keenan wrote:
>
> >Right now I'm looking at an older version of Test::Harness (v1.1604) and
I
> >don't see any code calling 'print "\n";'. There are several lines that
call
> >things like 'print $_' with no specific newline character, but I haven't
> >looked at it closely enough to see where the next newline is coming from.
>
> Could it be that $\ is set to "\n" or maybe something like it, for
> example, "\r"?
>
Quick search suggests $\ occurs nowhere within this version of
Test::Harness.
------------------------------
Date: 29 Apr 2003 23:35:06 -0700
From: adwser@hotmail.com (jtd)
Subject: Why is POE idle most of the time?
Message-Id: <c57c103.0304292235.2f7a1c0e@posting.google.com>
I wrote a simple test script using POE that created a single session
with a single handler that simply incremented a counter (0..1000) and
then yielded to itself. The script took 10s to run. When I profiled
it, dprofpp showed that the total user+system time was less than 0.8s.
What is happening here? Shouldn't POE use as much CPU time as the OS
scheduler allows?
Jtd
------------------------------
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.
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 4914
***************************************