[24582] in Perl-Users-Digest
Perl-Users Digest, Issue: 6758 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 2 03:05:47 2004
Date: Fri, 2 Jul 2004 00:05:08 -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, 2 Jul 2004 Volume: 10 Number: 6758
Today's topics:
Deleting blank elements from a list <goth1938@hotmail.com>
Re: Deleting blank elements from a list <invalid-email@rochester.rr.com>
Re: Deleting blank elements from a list <noreply@gunnar.cc>
Re: Deleting blank elements from a list <krahnj@acm.org>
Re: Looking for a certain regexp <nilram@hotpop.com>
Re: Looking for a certain regexp <pinyaj@rpi.edu>
Re: My 1st question about locking (and other related is <nilram@hotpop.com>
Re: Newbie: How to I extract word <josef.moellers@fujitsu-siemens.com>
Re: Q: re the kind of reference to be bless()ed in OO p <spamtrap@nowhere.com>
Re: shouldnt this evaluate in a scalar context??? <krahnj@acm.org>
Re: Why can't I upload file in my this CGI script? <komsbomb@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 2 Jul 2004 09:58:34 +0800
From: "Just in" <goth1938@hotmail.com>
Subject: Deleting blank elements from a list
Message-Id: <cc2fgb$vq0$1@news01.intel.com>
If I do this:-
use strict;
use warnings;
my @Arr1 = split /\D+/, $Str1;
my @Arr2 = split /\D+/, $Str2;
foreach my $i(0..$#Arr1)
{
foreach my $j(0..$#Arr2)
{
if($Arr1[$i] eq $Arr2[$j])
{
delete $Arr2[$j];
}
}
}
In some instances @Arr2 ends up looking like this ('', '', 'Value3',
Value4', '')
How can I make @Arr2 = ('Value3', 'Value4') i.e. $#Arr2 == 1
rather than 4?
Cheers,
Just in
------------------------------
Date: Fri, 02 Jul 2004 02:26:54 GMT
From: Bob Walton <invalid-email@rochester.rr.com>
Subject: Re: Deleting blank elements from a list
Message-Id: <40E4C7EE.50808@rochester.rr.com>
Just in wrote:
> If I do this:-
>
> use strict;
> use warnings;
>
> my @Arr1 = split /\D+/, $Str1;
> my @Arr2 = split /\D+/, $Str2;
>
> foreach my $i(0..$#Arr1)
> {
> foreach my $j(0..$#Arr2)
> {
> if($Arr1[$i] eq $Arr2[$j])
> {
> delete $Arr2[$j];
> }
> }
> }
>
> In some instances @Arr2 ends up looking like this ('', '', 'Value3',
> Value4', '')
>
> How can I make @Arr2 = ('Value3', 'Value4') i.e. $#Arr2 == 1
> rather than 4?
>
> Cheers,
> Just in
You could use the splice() function instead of delete() to remove
element from @Arr2 instead of just undef'ing them like delete() does:
perldoc -f splice
But you should really revamp your entire algorithm, since it is way slow
(order n^2). Maybe something like:
{
my %h;
@h{@Arr2}=(0..$#Arr2);
delete @h{@Arr1};
@Arr2=sort {$h{$a}<=>$h{$b}} keys %h;
}
You could apply the Schwarzian Transform to the sort to further improve
the efficiency.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Fri, 02 Jul 2004 04:28:12 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Deleting blank elements from a list
Message-Id: <2kjvicF361rgU1@uni-berlin.de>
Just in wrote:
> If I do this:-
>
> use strict;
> use warnings;
>
> my @Arr1 = split /\D+/, $Str1;
> my @Arr2 = split /\D+/, $Str2;
>
> foreach my $i(0..$#Arr1)
> {
> foreach my $j(0..$#Arr2)
To increase efficiency and prevent uninitialized warnings:
foreach my $j (reverse 0..$#Arr2)
> {
> if($Arr1[$i] eq $Arr2[$j])
> {
> delete $Arr2[$j];
splice @Arr2, $j, 1;
> }
> }
> }
>
> In some instances @Arr2 ends up looking like this ('', '',
> 'Value3', Value4', '')
>
> How can I make @Arr2 = ('Value3', 'Value4') i.e. $#Arr2 == 1
> rather than 4?
See suggestions above. You should also study
perldoc -q duplicate
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 02 Jul 2004 04:57:05 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Deleting blank elements from a list
Message-Id: <40E4EB0A.20BA9DD7@acm.org>
Just in wrote:
>
> If I do this:-
>
> use strict;
> use warnings;
>
> my @Arr1 = split /\D+/, $Str1;
> my @Arr2 = split /\D+/, $Str2;
You should probably use the match operator for that so that you don't
get an empty string as the first element of the array.
my @Arr1 = $Str1 =~ /\d+/g;
my @Arr2 = $Str2 =~ /\d+/g;
> foreach my $i(0..$#Arr1)
> {
> foreach my $j(0..$#Arr2)
> {
> if($Arr1[$i] eq $Arr2[$j])
> {
> delete $Arr2[$j];
> }
> }
> }
>
> In some instances @Arr2 ends up looking like this ('', '', 'Value3',
> Value4', '')
It would look more like (undef, undef, 'Value3', 'Value4', undef)
> How can I make @Arr2 = ('Value3', 'Value4') i.e. $#Arr2 == 1
> rather than 4?
This will do what you want:
my $regex = qr[\b(?:@{[ join '|', $Str1 =~ /\d+/g ]})\b];
my @Arr2 = grep !/$regex/, $Str2 =~ /\d+/g;
John
--
use Perl;
program
fulfillment
------------------------------
Date: 01 Jul 2004 21:05:05 -0500
From: Dale Henderson <nilram@hotpop.com>
Subject: Re: Looking for a certain regexp
Message-Id: <87pt7f5fla.fsf@camel.tamu-commerce.edu>
>>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
AS> Dale Henderson <nilram@hotpop.com> wrote in
>>
>> However, I wonder if could be done more along the lines of
>>
>> if ($cc=~/$re/){
>> print " -- Good\n";"
>> }
AS> Using the (?{ code }) and (??{ code }) constructs, it can.
AS> In particular, we can use (?{ code }) to build up the checksum
AS> in a global variable. Summing only the first 15 digits, we
AS> can predict the 16th and use (?{{ code }) to insert it as the
AS> expected match.
AS> our ( $sum, $odd);
AS> my $re = qr/
AS> (?: # repeat the following
AS> (\d) # match and capture a digit
AS> (?{ # increment $sum accordingly
AS> $sum += ( $odd ? $1 : ( $1 == 9) ? 9 : (2*$1)% 9);
AS> $odd = not $odd;
AS> })
AS> ){15} # repeat for 15 digits
AS> (??{ # predict and match 16th digit
AS> -$sum % 10;
AS> })
AS> /x;
AS> for ( qw( 1234567890123456 1234567890123452) ) {
AS> my $verdict = /$re/ ? 'Good' : 'Bad';
AS> print "$_ -- $verdict\n";
AS> }
AS> Of course, this still isn't a pure regex solution, it uses
AS> non-regex Perl code.
Perhaps not but this is still worthy of study which is the main
point of this exercise. I doubt that the regex solutions are very
efficient.
(Isn't that my mod 9 trick up there? :)
The ?{ code } and ??{ code } seem vaguely familiar. I couldn't
find anything about them in Camel 2 or "Mastering Regular
Expressions" (Owl) 1. So I read man page for perlre and found this
warning:
WARNING: This extended regular expression fea-
ture is considered highly experimental, and may
be changed or deleted without notice.
Since I'm running a fairly old version of perl (5.6.1), I wonder
if this warning is still in effect.
AS> Anno
--
Dale Henderson
"Imaginary universes are so much more beautiful than this stupidly-
constructed 'real' one..." -- G. H. Hardy
------------------------------
Date: Thu, 1 Jul 2004 23:43:58 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: Looking for a certain regexp
Message-Id: <Pine.SGI.3.96.1040701234230.52942B-100000@vcmr-64.server.rpi.edu>
On 1 Jul 2004, Dale Henderson wrote:
> The ?{ code } and ??{ code } seem vaguely familiar. I couldn't
> find anything about them in Camel 2 or "Mastering Regular
> Expressions" (Owl) 1. So I read man page for perlre and found this
> warning:
>
> WARNING: This extended regular expression fea-
> ture is considered highly experimental, and may
> be changed or deleted without notice.
>
> Since I'm running a fairly old version of perl (5.6.1), I wonder
> if this warning is still in effect.
They're still "highly experimental" in Perl 5.8.4, but they're here to
stay. Perl 6 will have them too.
In the mean time, you might want to check out an article I'm writing on
these two assertions:
http://japhy.perlmonk.org/articles/tpj/2004-summer.html
--
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: 01 Jul 2004 21:26:25 -0500
From: Dale Henderson <nilram@hotpop.com>
Subject: Re: My 1st question about locking (and other related issues)
Message-Id: <87lli35elq.fsf@camel.tamu-commerce.edu>
>>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
AS> Michele Dondi <bik.mido@tiscalinet.it> wrote in
AS> comp.lang.perl.misc:
AS> I wouldn't write the hostname in the lockfile but name the
AS> lockfile after the host. In fact, you don't need explicit
AS> file locking, the existence of the lockfile can be used. The
AS> usual problem with this approach is to reliably remove the
AS> lockfile when the program terminates. %SIG handlers and an
AS> END block cover most of that.
Another approach would be to write the pid into the
"lockfile". This would allow for the possibility of checking for
stale lock files although I do not know of any good way to check
for a process given a pid or verify it's the correct
process (i.e. not another process that just happens to have
inherited your pid).
This also allows for a "kill" script in the .bash_logout that
would open the "lockfile" and send an appropriate signal to the
specified pid. Of course this is problematic for it would kill
your sigfile "daemon" when the first shell exited. I presume you
would want the "daemon" to continue until the last shell exited.
What would be ideal (I think) is something like Debian's
start-stop-daemon but this is not portable. You might look into
your systems /etc/init.d/ scripts to see how daemons are started
and stopped.
AS> Anno
--
Dale Henderson
"Imaginary universes are so much more beautiful than this stupidly-
constructed 'real' one..." -- G. H. Hardy
------------------------------
Date: Fri, 02 Jul 2004 08:46:57 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Newbie: How to I extract word
Message-Id: <cc308c$1nt$1@nntp.fujitsu-siemens.com>
Dale Henderson wrote:
>>>>>>"GM" =3D=3D GM <geedotmuth@castcom.net> writes:
>=20
>=20
> GM> Mav wrote:
> >> Hi, there I got string like: $string=3D "------ Build started:
> >> Project: Myproject, Config: Debug ABC ------"; I would like
> >> print out only anything in between "Project:" to ",", in this
> >> case it is "Myproject" in perl. Any idea? Thanks, M
>=20
> GM> The following assumes you have no whitespace in your project
> GM> names:
>=20
> GM> my $project =3D $string =3D~ /Project: (\S+),/;
> =20
> The following makes no such assumption:
> =20
> my $project =3D $string =3D~ /Project: ([^,]+),/;
Nitpicking: That's not what Mav wrote and it's not even correct:
my $project;
($project =3D $string) =3D~ s/.*Project:([^,]+),.*/$1/;
At least with Perl v5.8.1
without the parentheses around the assignment, the value of $project is 1=
with the "my", I get "Can't declare scalar assignment in "my" at - line=20
2, near ") =3D~""
without the substitution, I get the entire string,
without the .*'s, I get another string.
You can also specify "non-greedyness":
($project =3D $string) =3D~ s/.*Project:(.*?),.*/$1/;
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
------------------------------
Date: Fri, 02 Jul 2004 04:52:04 GMT
From: Andrew Lee <spamtrap@nowhere.com>
Subject: Re: Q: re the kind of reference to be bless()ed in OO perl
Message-Id: <4bo9e0lu3ecokrjpourncdt22v9tbkdc0k@4ax.com>
On Fri, 02 Jul 2004 00:21:33 +0200, Michele Dondi <bik.mido@tiscalinet.it>
wrote:
>I must say in advance that I know the most basic aspects of OO
>programmin in Perl, but I've never been really proficient with it. So
>pardon my ignorance...
>
>It is often said that the reference that gets bless()ed to create an
>object is one to an anonymous hash, which makes perfectly sense, as
>the examples abunding everywhere clearly indicate.
>
>I've also seen bless()ing something like an empty {array,hash}ref to
>be used "solely" as an index into a package/class global hash to
>enforce some degree of encapsulation.
>
>Now I wonder if there are common/cool/smart/witty cases in which it is
>natural to use a reference to some other kind of object, like e.g. a
>sub, or filehandle, etc.
>
>
Certainly ... though I don't know how smart, cool or witty.
z.B.
#!/usr/bin/perl
use strict;
package Foo;
my $action = {
Default => \&Default,
PrintThis => \&PrintThis,
DumpENV => \&DumpENV
};
if ($action->{$ARGV[0]}) {
$action->{$ARGV[0]}->();
} else {
$action->{Default}->();
}
sub Default { print "no args?\n"; }
sub PrintThis {print "foo\n"; }
sub DumpENV {
for (keys %ENV) {
print "$_ => $ENV{$_}\n";
}
}
_END_
$ ./lookup_table.pl PrintThis
foo
$
*shrug*
Naturally you can bless $action ... which just means it can be used in another
Perl program like a static class member as $Foo::action
------------------------------
Date: Fri, 02 Jul 2004 01:22:17 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: shouldnt this evaluate in a scalar context???
Message-Id: <40E4B8B2.891918A5@acm.org>
dutone wrote:
>
> my %h = (roach=>'raid',slut=>'fun');
> my @a = qw(roach slut);
> print "YEA!!" if @h{@a} == @a
>
> Since @a is a list and evals to 2 in scalar context...
@a is an array, not a list.
perldoc -q "What is the difference between a list and an array"
> and @h{@a} returns a list, but this list doesnt go scalar.
@h{@a} is a hash slice and like the comma operator returns the
right-most element in scalar context.
> What I was trying to do is see if all the keys in a array
> (list,whatever)
> exist (not in the 'exists()' scence) in a hash. Of course there are
> alot of ways to do this, but i figured this method would work. Any of
> you perl thugs have any ideas?
The only way that I can think of at the moment is to use exists.
print "YEA!!" if @a == grep exists $h{$_}, @a;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 2 Jul 2004 10:42:49 +0800
From: "Koms Bomb" <komsbomb@hotmail.com>
Subject: Re: Why can't I upload file in my this CGI script?
Message-Id: <2kk08vF3b5sjU1@uni-berlin.de>
> enctype='multipart/form-data'
>
> shall be in the form tag.
Thank your very much, it works.
Seems I should learn more HTML syntax. :-)
--
Koms Bomb
*****Pardon my poor English*****
---------------------
My TASM homepage, resource for assembly. Tools, articles, links.
http://komsbomb.ols-lab.com
------------------------------
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 6758
***************************************