[16657] in Perl-Users-Digest
Perl-Users Digest, Issue: 4069 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Aug 19 21:10:47 2000
Date: Sat, 19 Aug 2000 18:10:15 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <966733815-v9-i4069@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 19 Aug 2000 Volume: 9 Number: 4069
Today's topics:
Re: Regex Alternation Question <elijah@workspot.net>
Re: Regex Alternation Question (Ilya Zakharevich)
Replace array element marxjkj123@my-deja.com
Re: Replace array element <Jonas.Reinsch@ppi.de>
Re: Replace array element <godzilla@stomp.stomp.tokyo>
Re: Replace array element <Jonas.Reinsch@ppi.de>
Re: Replace array element (Keith Calvert Ivey)
Re: Replace array element <lr@hpl.hp.com>
Re: Search and replace character sections (Keith Calvert Ivey)
Re: Using Win32::NetAdmin on remote machine <rico29@bellatlantic.net>
Web Solution Needed - Please respond <chaznsc@yahoo.com>
Re: Web Solution Needed - Please respond (Clinton A. Pierce)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 19 Aug 2000 19:32:16 GMT
From: Eli the Bearded <elijah@workspot.net>
Subject: Re: Regex Alternation Question
Message-Id: <eli$0008191510@qz.little-neck.ny.us>
In comp.lang.perl.misc, Rick Delaney <rick.delaney@home.com> wrote:
> Mark-Jason Dominus wrote:
> > In article <399BB4B9.B779C6F0@ppi.de>,
> > Jonas Reinsch <Jonas.Reinsch@ppi.de> wrote:
> > >The best solution I could think of is something like:
> > >for ($i = 0; $i<@parens; $i++) {
> > > if ($parens[$i]) {$part = $alt[$i];last}
> > >}
> > >Is there a more direct way to do this?
> With all your constraints, yes. With v5.6, anyway.
>
> > That's the only way I know of to do it, and when I've showed it in my
> > classes at the Perl conference nobody has ever suggested a better way.
>
> From the depths of perlvar, emerges:
>
> $part = $alt[$#- - 1];
>
> This is buried in the @- section of perlvar:
>
> One can use $#- to find the last matched subgroup in the last
> successful match. Contrast with $#+, the number of subgroups
> in the regular expression.
Oh Fun!
But it might not really help. I benchmarked this sort of thing
with an @alt array of about 210 regexps (not merely fixed strings)
against a 3.5 meg file, for which most lines would not have any
matches in @alt. I got my regexps from a mailfilter I used several
years ago, and the input file was a unix mbox.
Tested with perl5.6.0 on intel:
Benchmark: timing 5 iterations of...
prefilt: 1070 wallclock secs (993.11 usr + 23.33 sys = 1016.44 CPU)
prefilt_study: 1051 wallclock secs (990.64 usr + 22.96 sys = 1013.60 CPU)
tryall: 243 wallclock secs (222.74 usr + 5.69 sys = 228.43 CPU)
tryall_study: 228 wallclock secs (215.21 usr + 5.45 sys = 220.66 CPU)
atminus: 1012 wallclock secs (979.23 usr + 22.54 sys = 1001.77 CPU)
atminus_study: 1006 wallclock secs (981.93 usr + 22.08 sys = 1004.01 CPU)
Three routines, each with a version that uses study() and one that
does not. Prefilt has a single giant RE {$alt = join('|',@alt);
$alt = qr/($alt)/;} that gets applied to the line before applying
all individual REs, a technique I have found works well *sometimes*.
Tryall just loops over a regexp version of @alt
{@altre = map { $_ = qr/$_/ } @alt}. Atminus uses the prefilt giant
regexp and the value of $#-.
Here is the benchmarked code:
timethese(5, {
atminus_study => sub {
# line 1 "atminus_study"
open(IN, $file) or die;
while(<IN>) {
study($_);
if (/$alt/) {
$pattern = $#-;
}
}
close IN;
},
atminus => sub {
# line 1 "atminus"
open(IN, $file) or die;
while(<IN>) {
if (/$alt/) {
$pattern = $#-;
}
}
close IN;
},
prefilt_study => sub {
# line 1 "prefilt_study"
open(IN, $file) or die;
while(<IN>) {
if (/$alt/) {
$num = 0;
$match = $1;
study($match);
for $each (@altre) {
if ($match =~ /$each/) {
$pattern = $num;
last;
}
$num++;
}
}
}
close IN;
},
prefilt => sub {
# line 1 "prefilt"
open(IN, $file) or die;
while(<IN>) {
if (/$alt/) {
$num = 0;
$match = $1;
for $each (@altre) {
if ($match =~ /$each/) {
$pattern = $num;
last;
}
$num++;
}
}
}
close IN;
},
tryall_study => sub {
# line 1 "tryall_study"
open(IN, $file) or die;
while(<IN>) {
study($_);
$num = 0;
for $each (@altre) {
if (/$each/) {
$pattern = $num;
last;
}
$num++;
}
}
close IN;
},
tryall => sub {
# line 1 "tryall"
open(IN, $file) or die;
while(<IN>) {
$num = 0;
for $each (@altre) {
if (/$each/) {
$pattern = $num;
last;
}
$num++;
}
}
close IN;
},
});
I'd expect the regexps in @alt to vary the results considerably,
as well as the length of the strings being tested, and the
percentage of lines that have some match. Benchmarking with
your own patterns and data will help.
Elijah
------
could not get the (?{\$found=" . ++$i . '}) one to work
------------------------------
Date: 19 Aug 2000 22:53:16 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: Regex Alternation Question
Message-Id: <8nn34s$36a$1@charm.magnus.acs.ohio-state.edu>
[A complimentary Cc of this posting was sent to Eli the Bearded
<elijah@workspot.net>],
who wrote in article <eli$0008191510@qz.little-neck.ny.us>:
> > $part = $alt[$#- - 1];
> >
> > This is buried in the @- section of perlvar:
> >
> > One can use $#- to find the last matched subgroup in the last
> > successful match. Contrast with $#+, the number of subgroups
> > in the regular expression.
>
> Oh Fun!
>
> But it might not really help. I benchmarked this sort of thing
I coded these match-data things as special arrays to avoid the
giant overhead of an XSUB call. It turned out that access to special
arrays is significantly slower than an XSUB call....
Ilya
------------------------------
Date: Sat, 19 Aug 2000 18:11:12 GMT
From: marxjkj123@my-deja.com
Subject: Replace array element
Message-Id: <8nmijn$8ji$1@nnrp1.deja.com>
Hello all. I need an item within a string to be replaced.
Here is the situation.
$Line = "e:\dir1\environment\EJB\";
I need to replace "e:" within $Line with another drive letter, such
as "d:" and rewrite $Line to look like:
$Line = "d:\dir1\environment\EJB\";
I've tried tr/e:/d:/d; but no luck.
can anyone help.
thanks
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Sat, 19 Aug 2000 22:09:40 +0200
From: Jonas Reinsch <Jonas.Reinsch@ppi.de>
Subject: Re: Replace array element
Message-Id: <399EE984.E4FE38F5@ppi.de>
marxjkj123@my-deja.com wrote:
>
> Hello all. I need an item within a string to be replaced.
>
> Here is the situation.
>
> $Line = "e:\dir1\environment\EJB\";
This assignment doesn't work, because the last backslash escapes
the doublequote. Use:
$Line = "e:\dir1\environment\EJB\\";
>
> I need to replace "e:" within $Line with another drive letter, such
> as "d:" and rewrite $Line to look like:
>
> $Line = "d:\dir1\environment\EJB\";
See above.
>
> I've tried tr/e:/d:/d; but no luck.
>
> can anyone help.
>
> thanks
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
Given you did the assignment right, the following will do:
my $newdrive = "d:";
$Line =~ s/^e:/$newdrive/;
Jonas.
------------------------------
Date: Sat, 19 Aug 2000 13:02:06 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Replace array element
Message-Id: <399EE7BE.60833D29@stomp.stomp.tokyo>
marxjkj123@my-deja.com wrote:
> Hello all. I need an item within a string to be replaced.
> $Line = "e:\dir1\environment\EJB\";
> I need to replace "e:" within $Line with another drive letter, such
> as "d:" and rewrite $Line to look like:
> $Line = "d:\dir1\environment\EJB\";
> I've tried tr/e:/d:/d; but no luck.
> can anyone help.
What does this have to do with an array element?
You won't like my help. I am exceptionally skeptical
of your statements. As you know, your current format
for $Line will cause a script to crash. How could you
possibly test methods with a script which won't run?
* flat palm smacks her forehead *
Duh?
Sheesshh... such an impotent mindgame.
Post your operational test script. I want
to see it with,
$Line = "e:\dir1\environment\EJB\";
intact and exact as you state.
TEST SCRIPT:
____________
#!/usr/local/bin/perl
print "Content-Type: text/plain\n\n";
$line = "e:\\dir1\\environment\\EJB\\";
print "INPUT: $line\n\n";
$drive_letter = substr ($line, 0, 1, "d");
print "OUTPUT: $line";
$line = "e:/dir1/environment/EJB/";
print "\n\n\nINPUT: $line\n\n";
$drive_letter = substr ($line, 0, 1, "d");
print "OUTPUT: $line";
print "\n\n\nGodzilla Rocks!";
exit;
PRINTED RESULTS:
________________
INPUT: e:\dir1\environment\EJB\
OUTPUT: d:\dir1\environment\EJB\
INPUT: e:/dir1/environment/EJB/
OUTPUT: d:/dir1/environment/EJB/
Godzilla Rocks!
------------------------------
Date: Sat, 19 Aug 2000 22:27:52 +0200
From: Jonas Reinsch <Jonas.Reinsch@ppi.de>
Subject: Re: Replace array element
Message-Id: <399EEDC8.819EDB1B@ppi.de>
> This assignment doesn't work, because the last backslash escapes
> the doublequote. Use:
>
> $Line = "e:\dir1\environment\EJB\\";
Of course I meant:
$Line = "e:\\dir1\\environment\\EJB\\";
(I thinked of singlequotes; sorry).
Jonas.
------------------------------
Date: Sat, 19 Aug 2000 20:54:25 GMT
From: kcivey@cpcug.org (Keith Calvert Ivey)
Subject: Re: Replace array element
Message-Id: <39a5ef2f.25784572@news.newsguy.com>
marxjkj123@my-deja.com wrote:
>$Line = "e:\dir1\environment\EJB\";
That doesn't compile, and even if you fixed the end so that the
quoted string was terminated it wouldn't contain what you think
it does, since "\e" is an escape character (chr 27) and "\d" and
"\E" are just "d" and "E". Use
$Line = "e:\\dir1\\environment\\EJB\\";
or
$Line = 'e:\dir1\environment\EJB\\';
or (best, since you don't have to worry about backslashes)
$Line = 'e:/dir1/environment/EJB/';
>I need to replace "e:" within $Line with another drive letter, such
>as "d:" and rewrite $Line to look like:
>
>$Line = "d:\dir1\environment\EJB\";
>
>I've tried tr/e:/d:/d; but no luck.
You really have to read the documentation rather than just
trying random things. You're asking to change (in $_, unless
you've put a '$Line =~' before it that you haven't shown us) all
occurrences of 'e' to 'd' and all occurrences of ':' to ':' (why
bother?) and delete all characters in the search list that don't
have a corresponding character in the replacement list (there
are none, since each list has exactly two characters in this
case, so the /d is useless). Please look at the documentation
for s/// in perlop, because you probably want something like
$Line =~ s/^e:/d:/;
But from the look of things I'd say you have some reading to
do before you're going to get far with your program. I'm not
saying that to be mean, but because it's better for you to
understand it as soon as possible. Good luck!
--
Keith C. Ivey <kcivey@cpcug.org>
Washington, DC
------------------------------
Date: Sat, 19 Aug 2000 15:36:56 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Replace array element
Message-Id: <MPG.1408abdfa620b34998aca7@nntp.hpl.hp.com>
In article <8nmijn$8ji$1@nnrp1.deja.com>, marxjkj123@my-deja.com says...
> Hello all. I need an item within a string to be replaced.
Why does your Subject refer to an 'array element'? Perl strings aren't
arrays.
> Here is the situation.
>
> $Line = "e:\dir1\environment\EJB\";
Others have pointed out the syntax errors in that statement, but haven't
bothered to let you in on the Big Secret. Forward slashes work just
fine, and are far easier to manage. You can use single- or double-
quotes, though single-quotes are more appropriate for simple literals.
> I need to replace "e:" within $Line with another drive letter, such
> as "d:" and rewrite $Line to look like:
>
> $Line = "d:\dir1\environment\EJB\";
>
> I've tried tr/e:/d:/d; but no luck.
Did you try reading the documentation for the tr() operator?
> can anyone help.
You can help yourself, by reading Perl tutorials or introductory books;
by reading the documentation before guessing how a function works; and
if all else fails, by posting code that at least compiles correctly,
even if it doesn't do what you hoped.
Oh, BTW,
$Line =~ s/e/d/;
or, if you want to be super-secure:
$Line =~ s/^e:/d:/;
Read perlop and perlre for details.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Sat, 19 Aug 2000 21:43:59 GMT
From: kcivey@cpcug.org (Keith Calvert Ivey)
Subject: Re: Search and replace character sections
Message-Id: <39a8fe4f.29657436@news.newsguy.com>
Uri Guttman <uri@sysarch.com> wrote:
>look, ma, ahead! (and no ugly expression ref interpolation!)
>
> $addr =~ s/[a-z](?=[a-z]*\.)/*/g ;
>
>it makes assumptions but it works fine.
Note that the assumptions include two dubious ones: (1) that the
host name will never include digits or hyphens and (2) that the
user name will never include periods.
>here is the same with a
>lookbehind check to make sure the domain followed a @. it needs the
>while loop to deal with lookbehind not handling variable length
>patterns. not no /g modifier
>
> 1 while $addr =~ s/(?<=[\@*])[a-z](?=[a-z]*\.)/*/'
That (once the leftover ' is removed) no longer requires
assumption 2 but means that addresses not meeting assumption 1
are no longer even partially obscured. Moreover, host names
with more than two parts (like mail.example.com) will have only
the least important part obscured.
Changing the repeated character class would solve those
problems:
1 while $addr =~ s/(?<=[\@*])[^*@](?=[^*@]*\.)/*/;
That should suffice as long as Eli the Bearded doesn't have a
son with the address *.the.younger@qz.to. I don't know that I
find it more esthetically pleasing than the solutions with /e
and length(), though.
--
Keith C. Ivey <kcivey@cpcug.org>
Washington, DC
------------------------------
Date: Sat, 19 Aug 2000 23:36:21 GMT
From: "K Johnson" <rico29@bellatlantic.net>
Subject: Re: Using Win32::NetAdmin on remote machine
Message-Id: <VREn5.1143$Q%5.50890@typhoon2.ba-dsg.net>
try $server = "\\\\".servername;
Homer Simpson <homer.simpson@springfield.nul> wrote in message
news:8nl66p$r2e$0@216.39.130.27...
>
>
> Greetings. I'm experimenting with NetAdmin, specifically the function
> UserGetAttributes.
>
> Using the example from Learning Perl on Win32 Systems I've been able to
get
> data into:
>
>
>
> UserGetAttributes("", $userName, $password, $passwordAge, $privilege,
> $homeDir, $comment, $flags, $scriptPath)
>
>
> and print it out to the screen.
>
> The docs tell me that the first item ("" in the example code from the
Gecko
> book) is optional and the machine running the script will be used if it's
not
> supplied...
> That's certainly true if I put the machine name in a variable or if I
leave
> the first part blank I get data from the machine I'm running the script
on...
>
> If I put in another machinename I get errors indicating that my print
> statements have uninit'ed variables in them...
>
> I've tried replacing "" with $server and setting $server to:
> "\\\\SERVERNAME"
> "\\SERVERNAME" (wrong I know but I was getting frustrated)
> "SERVERNAME"
> "servername"
> "domainname\\servername"
>
> If I put in the name of the server the script is running on it works fine.
>
> I'm a Domain Admin so it's not a rights issue (is it?).
>
> Can someone point me to the sentance or paragraph I've overlooked?
>
> thanks
------------------------------
Date: Sat, 19 Aug 2000 17:41:25 -0400
From: chaz <chaznsc@yahoo.com>
Subject: Web Solution Needed - Please respond
Message-Id: <399EFF05.2246@yahoo.com>
I host a web site for a local auto dealer who wants to place his
pre-owned inventory on the web.
What I need is an "engine" of sorts that can read data from a text based
database file. Please send me your input, proposals, etc. I can provide
more elaboration once we make initial contact.
Thanks
chaz-
------------------------------
Date: Sat, 19 Aug 2000 22:50:30 GMT
From: clintp@geeksalad.org (Clinton A. Pierce)
Subject: Re: Web Solution Needed - Please respond
Message-Id: <WaEn5.4538$QW4.75127@news1.rdc1.mi.home.com>
In article <399EFF05.2246@yahoo.com>,
chaz <chaznsc@yahoo.com> writes:
> I host a web site for a local auto dealer who wants to place his
> pre-owned inventory on the web.
>
> What I need is an "engine" of sorts that can read data from a text based
> database file. Please send me your input, proposals, etc. I can provide
> more elaboration once we make initial contact.
In Unix, try "cat". In Windows try "type". If you're stranded with just
perl, try:
perl -pe 1 <your_database_here>
I'll send you a bill for consulting services rendered. Hourly charges
are $150 plus expenses. I type slowly and this took about 3 hours to
type in and test. And I ate dinner too. An expensive one.
--
Clinton A. Pierce Teach Yourself Perl in 24 Hours!
clintp@geeksalad.org for details see http://www.geeksalad.org
"If you rush a Miracle Man,
you get rotten Miracles." --Miracle Max, The Princess Bride
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 4069
**************************************