[19706] in Perl-Users-Digest
Perl-Users Digest, Issue: 1901 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 10 03:05:31 2001
Date: Wed, 10 Oct 2001 00:05:12 -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: <1002697511-v10-i1901@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 10 Oct 2001 Volume: 10 Number: 1901
Today's topics:
Re: Accessing Public Scripts <goldbb2@earthlink.net>
Re: binmode with "perl -p" doesn't work <pne-news-20011010@newton.digitalspace.net>
Re: CGI.pm :: OO or not? <pne-news-20011010@newton.digitalspace.net>
errors when i use 2 modules <troyr@vicnet.net.au>
Re: errors when i use 2 modules <pne-news-20011010@newton.digitalspace.net>
Re: fork() question <goldbb2@earthlink.net>
Re: getting Perl to wait <goldbb2@earthlink.net>
Grouped column sum <dgregory@mmcable.com>
Re: Grouped column sum <krahnj@acm.org>
Re: Grouped column sum <mbudash@sonic.net>
Re: Grouped column sum (Damian James)
Re: HELP: Simple Pattern Matching Problem (Ian Boreham)
Re: How do I... (John J. Trammell)
Re: integer division <pne-news-20011010@newton.digitalspace.net>
minor correction: string literal vs. string variable pr <ted_godwin@mindspring.com>
Re: Modification date of an external file (Martien Verbruggen)
Re: Newbie: Net::FTP, putting more than one file at a t <goldbb2@earthlink.net>
pointers and perl (bal)
Re: pointers and perl <uri@sysarch.com>
Re: pointers and perl (Mark Jason Dominus)
Re: regex substitution (Ian Boreham)
Re: Signaling <goldbb2@earthlink.net>
Re: sort of array containing strings doesn't work (Fredrik)
Sort unique Very large file <jzh@earthlink.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 09 Oct 2001 22:26:36 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Accessing Public Scripts
Message-Id: <3BC3B1DC.B8C07FE4@earthlink.net>
Andrea wrote:
>
> I don't know WHAT possessed me to choose such a non-descriptive
> subject line for my post. Sorry.
Ooh, I know what possessed you!
"Out, out, you demons of stupidity!" :)
--
"Just how stupid are you Kuno?"
"Verily, Tatewaki Kuno knows no limits."
------------------------------
Date: Wed, 10 Oct 2001 07:59:47 +0200
From: Philip Newton <pne-news-20011010@newton.digitalspace.net>
Subject: Re: binmode with "perl -p" doesn't work
Message-Id: <9no7stkatpf4r69fqn4bi1m14v3mhqt23p@4ax.com>
On Mon, 08 Oct 2001 07:33:32 GMT, mjd@plover.com (Mark Jason Dominus)
wrote:
> It seems to me you could write a module that would do this.
One such module is Jenda Krynicky's G.pm ( http://Jenda.Krynicky.cz/#G ,
http://Jenda.Krynicky.cz/perl/G.pm.html ).
> Then you would say
>
> perl -MGPP -e "print @ARGV" *.pl *.txt *.cpp
>
> or you could put 'use GPP;' at the top of your program.
Or add '-MG' to PERL5OPT and never have to worry about it any more (as
suggested on Jenda's page).
> GPP.pm would be about two lines long.
G.pm is a bit longer, but it also handles a few other things, for
example, translating `foo` into the output of the command foo, and
translating 'foo bar' into, um 'foo bar' (the difference here is whether
the program sees (q<'foo>, q<bar'>), which is what the shell passes, or
(q<foo bar>), which is what G.pm produces, in its @ARGV).
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Wed, 10 Oct 2001 08:40:15 +0200
From: Philip Newton <pne-news-20011010@newton.digitalspace.net>
Subject: Re: CGI.pm :: OO or not?
Message-Id: <s6r7st4r3jv7cr1eacp0r9b7n2qi400vmp@4ax.com>
On Tue, 09 Oct 2001 12:04:59 -0400, Lou Moran <lmoran@wtsg.com> wrote:
> --I just started using CGI.pm and I have noticed that while perldoc
> CGI recomends OO funtionality but I rarely see it in use.
I heard a rumour that LDS also recommends using the function-based
approach, but that would make it hard to explain why the docs don't do
it that way.
> --So if --*YOU*-- were using CGI.pm to accomplish some task would you
> write it utilizing the OO approach or the function based approach?
I use the function based approach.
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Wed, 10 Oct 2001 15:44:01 +1000
From: "tez" <troyr@vicnet.net.au>
Subject: errors when i use 2 modules
Message-Id: <ggRw7.2439$6q1.133661@ozemail.com.au>
Hi There...
I get a perl warning/error when i use the following modules
#!/usr/bin/perl
use CGI qw(:standard);
use LWP::Simple;
Basically a script which just contains those 3 lines brings up the following
error
error
Prototype mismatch: sub main::head vs ($) at ./test.cgi line 7
The program continues to work but is filling up the web servers error log.
Is there a way i can get around this and still use the same modules?
Thanks in advance
--
----------------------------------------------------------------
Troy Rasiah
Melbourne, Aus
------------------------------
Date: Wed, 10 Oct 2001 08:43:44 +0200
From: Philip Newton <pne-news-20011010@newton.digitalspace.net>
Subject: Re: errors when i use 2 modules
Message-Id: <9dr7st8vkmdg3phnmcu0420lv2fvg9s75r@4ax.com>
On Wed, 10 Oct 2001 15:44:01 +1000, "tez" <troyr@vicnet.net.au> wrote:
> Hi There...
> I get a perl warning/error when i use the following modules
>
> #!/usr/bin/perl
> use CGI qw(:standard);
> use LWP::Simple;
>
> Basically a script which just contains those 3 lines brings up the following
> error
>
> error
> Prototype mismatch: sub main::head vs ($) at ./test.cgi line 7
Looks like both CGI and LWP::Simple export a subroutine called head.
> The program continues to work but is filling up the web servers error log.
> Is there a way i can get around this and still use the same modules?
Yes, specify explicitly what you want to export rather than "all
'standard' exports in CGI and all default exports in LWP::Simple". For
example, if all you use from LWP::Simple is 'get', then you could
use LWP::Simple qw(get);
. Or you could narrow down your import from CGI if you're using
LWP::Simple's head subroutine. Or, if you want to use *both*
LWP::Simple::head and CGI's head, you could switch to the OO interface
of CGI.pm.
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Tue, 09 Oct 2001 23:11:03 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: fork() question
Message-Id: <3BC3BC47.7758A6D4@earthlink.net>
Aaron Wright wrote:
>
> Hello. I have a question about using fork(). The code and examples I
> refer to are included below.
>
> Basically I'm forking an exec call because sometimes that call hangs.
The code you have is rather ugly, and as you've noticed, doesn't solve
your problem. If you are certain that the program will produce output
if it succeeds, then run it like this:
defined( my $pid = open SMBCLIENT, "-|" )
or die "Either pipe or fork failed: $!";
if( $pid == 0 ) {
open(STDIN, "<ftpcommand.tmp")
or die "Couldn't open ftpcommand.tmp for read: $!";
#exec "smbclient", qq(//"$smbname"/"$shareline"), "-I", $ip, "-N";
exec "smbclient", "//$smbname/$shareline", "-I", $ip, "-N";
# since exec LIST shouldn't get parsed by the shell, $smbname and
# $shareline shouldn't get broken into parts by spaces, even if
# they don't have extra quotes.
die "Couldn't exec smbclient: $!";
}
use IO::Select;
if( () = IO::Select->new(*SMBCLIENT)->can_read(15) ) {
print "Child process is *not* hung.\n";
local $| = 1; # turn on autoflush
my $blocksize = (stat(SMBCLIENT))[11] || 4096;
print while sysread(SMBCLIENT, $_, -s SMBCLIENT || $blocksize);
} else {
print "Child process appears to be hung; killing it.\n";
kill( KILL => $pid );
}
unless( close SMBCLIENT ) {
my ($sig, $ret) = ($?&255, $?>>8);
die "Either close or waitpid failed: $!"; if $!;
die "smbclient died from signal $sig" if $sig;
die "smbclient exited with code $ret" if $ret;
}
unlink("processlist.tmp") or die "Couldn't remove processlist.tmp: $!";
NB: this code is untested.
--
"Just how stupid are you Kuno?"
"Verily, Tatewaki Kuno knows no limits."
------------------------------
Date: Wed, 10 Oct 2001 00:21:04 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: getting Perl to wait
Message-Id: <3BC3CCB0.9BF4DDF7@earthlink.net>
Milan Göllner wrote:
>
> Well, I've managed to create a User with Expect.pm remote
> controlling passwd, works quite nicely that.
>
> BUT .... the whole thing is based on something like this:
>
> -----
> main
> do_some_work
> call sub1()
>
> sub1
> do_some_work
> call sub2()
>
> sub2
> finish_work
> return_to_main()
> -----
>
> main is one big while block. This whileblock doesn't seem to wait
> for its various subs to finish their work. It rather seems to feed
> new data into the various subs which effectively confuses my last
> sub (the one with the Expect.pm controlling passwd) and feeds wrong
> data to passwd.
>
> I Believe this might be solved with ait, unfortunately I don't get
> it ... how is it used ?
You haven't given us alot of info... I think you'll have to show us
your script for us to help you.
--
"Just how stupid are you Kuno?"
"Verily, Tatewaki Kuno knows no limits."
------------------------------
Date: Wed, 10 Oct 2001 04:51:33 GMT
From: "Dustin Gregory" <dgregory@mmcable.com>
Subject: Grouped column sum
Message-Id: <ptQw7.99784$ME2.15234736@typhoon.kc.rr.com>
I've seen some posts that had examples that almost had the solution to my
problem, but being the newbie I am, I couldn't figure out how to do this.
Here's an example of what data I have and what I'm trying to do with it.
What I have:
group1 15
group2 2
group5 4
group2 18
group4 20
group3 9
group3 6
group1 2
What I'm trying to get: (group = sum)
group1 = 17
group2 = 20
group3 = 15
group4 = 20
group5 = 4
I've looked around with no luck on a solution. Any help would be
appreciated.
Thanks in adv,
-dg
------------------------------
Date: Wed, 10 Oct 2001 06:05:16 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Grouped column sum
Message-Id: <3BC3E5A2.F6AA8050@acm.org>
Dustin Gregory wrote:
>
> I've seen some posts that had examples that almost had the solution to my
> problem, but being the newbie I am, I couldn't figure out how to do this.
> Here's an example of what data I have and what I'm trying to do with it.
>
> What I have:
>
> group1 15
> group2 2
> group5 4
> group2 18
> group4 20
> group3 9
> group3 6
> group1 2
>
> What I'm trying to get: (group = sum)
>
> group1 = 17
> group2 = 20
> group3 = 15
> group4 = 20
> group5 = 4
>
> I've looked around with no luck on a solution. Any help would be
> appreciated.
#!/usr/bin/perl -w
use strict;
my %hash;
while ( <DATA> ) {
chomp;
$hash{ (split)[0] } += (split)[1];
}
for my $key ( sort keys %hash ) {
print "$key = $hash{$key}\n";
}
__DATA__
group1 15
group2 2
group5 4
group2 18
group4 20
group3 9
group3 6
group1 2
John
--
use Perl;
program
fulfillment
------------------------------
Date: Wed, 10 Oct 2001 06:19:04 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Grouped column sum
Message-Id: <mbudash-BB6F31.23191009102001@news.sonic.net>
In article <ptQw7.99784$ME2.15234736@typhoon.kc.rr.com>, "Dustin
Gregory" <dgregory@mmcable.com> wrote:
> I've seen some posts that had examples that almost had the solution to my
> problem, but being the newbie I am, I couldn't figure out how to do this.
> Here's an example of what data I have and what I'm trying to do with it.
>
> What I have:
>
> group1 15
> group2 2
> group5 4
> group2 18
> group4 20
> group3 9
> group3 6
> group1 2
>
>
> What I'm trying to get: (group = sum)
>
> group1 = 17
> group2 = 20
> group3 = 15
> group4 = 20
> group5 = 4
>
> I've looked around with no luck on a solution. Any help would be
> appreciated.
>
> Thanks in adv,
> -dg
>
>
undef %totals;
while (<DATA>) {
my ($group, $value) = split;
$totals{$group} += $value;
}
foreach (sort keys %totals) {
print "$_ = $totals{$_}<br>\n";
}
__END__
group1 15
group2 2
group5 4
group2 18
group4 20
group3 9
group3 6
group1 2
hope this helps,
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: 10 Oct 2001 06:37:32 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: Grouped column sum
Message-Id: <slrn9s7qvo.194.damian@puma.qimr.edu.au>
On Wed, 10 Oct 2001 04:51:33 GMT, Dustin Gregory said:
>I've seen some posts that had examples that almost had the solution to my
>problem, but being the newbie I am, I couldn't figure out how to do this.
>Here's an example of what data I have and what I'm trying to do with it.
>
>What I have:
>
>group1 15
>group2 2
>group5 4
>group2 18
>group4 20
>group3 9
>group3 6
>group1 2
>
>
>What I'm trying to get: (group = sum)
>
>group1 = 17
>group2 = 20
>group3 = 15
>group4 = 20
>group5 = 4
>
Assuming the data is in 'filename':
perl -lane '$i{$F[0]}+=$F[1]}{print"$_ = $i{$_}" for keys %i' filename
Anyone for golf?
Cheers,
Damian
--
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub
------------------------------
Date: 9 Oct 2001 20:56:40 -0700
From: ianb@ot.com.au (Ian Boreham)
Subject: Re: HELP: Simple Pattern Matching Problem
Message-Id: <f02c4576.0110091956.440d73c2@posting.google.com>
"George Vandyke" <gvandyke@voyager.net> wrote in message news:<3bc37947$0$18899$272ea4a1@news.execpc.com>...
>Hello All,
>I have a problem parsing the following text string:
>"Team.ScratchDirectory: d:\Ideas\Team_8\Scratch"
>What I'd like to do is :
> 1. find the position of the first ":" in the string.
> 2. use substr($string, $colon_loc) to extract everything to the right
> of the first colon. Know how to do this part.
> 3. Remove the leading whitespace from the extracted text string (the
> whitespace between the first ":" and the "d:")
>Q: Is there a Perl function which returns the location in a string,
of the
> match to an expression or character? i.e.,
> $string = "123456789";
> $match = strloc($string, "7"); # returns the location 7 or 6
index, rindex
>Q: How do you remove all the leading whitespace out of a string?
>" d:\IDEAS\test.txt" -> "d:\IDEAS\test.txt"
FAQ: "How do I strip blank space from the beginning/end of a string?"
---
If I were you, I'd combine them all into a single regex match:
#!/usr/bin/perl -w
$line = "Team.ScratchDirectory: d:\Ideas\Team_8\Scratch";
if ($line =~ /:\s*(.*)/)
{
print "Found '$1'\n";
}
This could be modified to ignore trailing whitespace too. Your
original technique was very C-like. This sort of processing can be
done much more simply (for the coder and maintainer) using regexes.
Regards,
Ian
------------------------------
Date: Tue, 9 Oct 2001 22:01:49 -0500
From: trammell@haqq.hypersloth.invalid (John J. Trammell)
Subject: Re: How do I...
Message-Id: <slrn9s7egt.q7n.trammell@haqq.hypersloth.net>
On Tue, 9 Oct 2001 20:41:37 -0400, Aaron Wright <wright.546@osu.edu> wrote:
> Here's what I'm trying to do:
> I set $p = substr($line,0,1). Then I want to test if $p is equal to a
> backslash, or \ .
[ haqq ~ ] perl -lne 'print "backslash!" if $_ eq "\\"'
foo
\
backslash!
\\
\
backslash!
^D
[ haqq ~ ]
--
Take LISP, make the syntax twice as annoying, and hey presto, XML!
------------------------------
Date: Wed, 10 Oct 2001 08:06:31 +0200
From: Philip Newton <pne-news-20011010@newton.digitalspace.net>
Subject: Re: integer division
Message-Id: <nap7stkkijadkae3jq368oqjto20386rdl@4ax.com>
On Sun, 07 Oct 2001 23:29:58 -0400, Benjamin Goldberg
<goldbb2@earthlink.net> wrote:
> Or use sprintf.
That would result in 12, rather than 11.
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Tue, 9 Oct 2001 21:47:23 -0400
From: "TedWeb" <ted_godwin@mindspring.com>
Subject: minor correction: string literal vs. string variable problem...
Message-Id: <9q09f5$14j$1@slb3.atl.mindspring.net>
CORECTION: the perl version reads this:
$file->SaveAs($fileName);
my apologies... the motor's running but no one's at the wheel (I'm
exhausted).
"TedWeb" <ted_godwin@mindspring.com> wrote in message
news:9q0986$qgj$1@slb7.atl.mindspring.net...
> Hello everyone,
>
> I've been translating an ASP VBScript file to PerlScript and have one
> bizarre problem. The VBScript reads this:
> file.SaveAs(fileName)
>
> In PerlScript it reads:
> $file.SaveAs($fileName);
>
> The problem is that the Perl version won't accept a string variable.
> However, if I typed:
> $file.SaveAs('$fileName'); it would work, only using the incorrect literal
> value. Any help?
>
> Many thanks,
> -Ted
>
>
------------------------------
Date: Wed, 10 Oct 2001 01:32:06 GMT
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Modification date of an external file
Message-Id: <slrn9s798m.l88.mgjv@verbruggen.comdyn.com.au>
On Wed, 10 Oct 2001 01:38:44 +0200,
Daniel Neubert <News@DanielNeubertSoftware.de> wrote:
> Hi everybody !
>
> I´m looking for a simple cgi/perl script which returns the date of last
> modification of a file specified this way:
>
> /cgi-bin/lastmod.cgi?../features/directory/file.ext
>
> The output should be formated in DD.MM.YYYY.
>
> If anybody could write this (obviously very small and simple script) -
If it's that obvioulsy small and simple, why don't you write it
yourself?
> please answer and/or mail me !
I'm sure you'll get lots of offers with rate cards included.
> Daniel Neubert
> Daniel Neubert Software
As in "I work for a software company, but can't pull this one off"?
Martien
--
Martien Verbruggen |
Interactive Media Division | Unix is user friendly. It's just
Commercial Dynamics Pty. Ltd. | selective about its friends.
NSW, Australia |
------------------------------
Date: Wed, 10 Oct 2001 01:10:01 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Newbie: Net::FTP, putting more than one file at a time
Message-Id: <3BC3D829.64CAF08F@earthlink.net>
Clayton wrote:
>
> I've got $ftp->put($filename) working but I want to mput instead. How
> do I do this? Thanks in advance.
mput is just a repeated put.
foreach my $mput_filename ( glob "*.html" ) {
$ftp->put( $mput_filename );
}
Adding error checking is an exercise for the student.
--
"Just how stupid are you Kuno?"
"Verily, Tatewaki Kuno knows no limits."
------------------------------
Date: 9 Oct 2001 20:33:57 -0700
From: balsellathurai@hotmail.com (bal)
Subject: pointers and perl
Message-Id: <58504532.0110091933.4b26f62@posting.google.com>
Hi
I am new to perl so please forgive me if this a is a silly question! I
came across some code which goes as follows:
use strict;
use DBI;
# Connect to the database
my $dbh = DBI->connect('DBI:mysql:my_database', 'my_username', 'my_pas
+sword')
or die "Couldn't open database: $DBI::errstr; stopped";
etc....
....etc....
Because of the presence of -> does this mean pointers, linked lists
are feasible in perl?
Thanks!
Bal
------------------------------
Date: Wed, 10 Oct 2001 03:42:23 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: pointers and perl
Message-Id: <x7669o88jg.fsf@home.sysarch.com>
>>>>> "b" == bal <balsellathurai@hotmail.com> writes:
b> my $dbh = DBI->connect('DBI:mysql:my_database', 'my_username', 'my_pas
b> +sword')
b> Because of the presence of -> does this mean pointers, linked lists
b> are feasible in perl?
you are badly mistaken. perl doesn't support pointers. and the use of ->
doesn't imply that.
but, perl does have references which are smarter and safer versions of
pointers. and you rarely see classic linked lists in perl programs as
perl arrays can do that more easily.
get a decent beginning book on perl (learning perl, elements of
programming in perl) and read it.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs -------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 10 Oct 2001 04:14:40 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: pointers and perl
Message-Id: <3bc3cb30.42a9$1fc@news.op.net>
In article <58504532.0110091933.4b26f62@posting.google.com>,
bal <balsellathurai@hotmail.com> wrote:
>my $dbh = DBI->connect('DBI:mysql:my_database', 'my_username', 'my_pas
>+sword')
> or die "Couldn't open database: $DBI::errstr; stopped";
>
>etc....
>....etc....
>
>Because of the presence of -> does this mean pointers,
No. -> is a method call.
> linked lists are feasible in perl?
Perl has references, and linked lists are possible in Perl.
See perlfaq4, "How do I handle linked lists?"
It is extremely rare for linked lists to be a good solution to a Perl
programming problem. It is almost always preferable to use an array.
Linked lists are slower than arrays and use more memory. Their only
benefit is that they can expand dynamically to hold any amount of
data. Arrays in C cannot do this.
However, in Perl, arrays do not have a fixed size, and expand
dynamically, so are usually suitable in place of linked lists.
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: 9 Oct 2001 18:44:59 -0700
From: ianb@ot.com.au (Ian Boreham)
Subject: Re: regex substitution
Message-Id: <f02c4576.0110091744.70fc1f8@posting.google.com>
"fishy" <joerivdv007@hotmail.com> wrote in message news:<DkEw7.94987$6x5.20798053@afrodite.telenet-ops.be>...
> Hi,
>
> I want to substitute a regexp in a text.
> E.g.
> while (<>) {
> if ($_ =~ /(^| |\.)(B|b)ij(\n| |\.)/) {
> s/(^| |\.)(B|b)ij(\n| |\.)/ /g;
> }
> }
>
> The problem is that this only substitutes the first
> appearance of the regexp and ignores any further instances of that
> regexp on the same line. So if the line says:
>
> bij de bakker en niet bij de kapper
>
> Then the result is:
>
> de bakker en niet bij de kapper
I modified your code to make a runnable script, but did not touch the
match or substitution:
#!/usr/bin/perl -w
foreach (<DATA>) {
if ($_ =~ /(^| |\.)(B|b)ij(\n| |\.)/) {
s/(^| |\.)(B|b)ij(\n| |\.)/ /g;
print;
}
}
__DATA__
bij de bakker en niet bij de kapper
The output I got was:
de bakker en niet de kapper
which appears to be what you wanted. Please post runnable code, and
copy it, don't retype it. I suspect you just left of the /g in the
original, or you corrected something else in retyping it.
> I want both instances of 'bij' deleted from the line. Is there some way of
> doing this the right way?
Well, if you want to know "the right way", there are a number of
improvements you could make. Unfortunately, it depends on what your
requirements are:
. Do you simply want to remove all instances of the word "bij"? You
probably want to use word boundaries (\b) for this. There's an FAQ on
that.
. What do you want to happen to the surrounding spaces? Currently you
are adding (leaving?) a space at the start of the string if you do a
replacement there. Ideally, you would only leave a space between two
other words, but not at the start of a string, and not before a
punctuation mark (at least in English).
Incidentally, (B|b) is an expensive way of writing [Bb]. $_ is the
default string for matching and substitution, so you don't need to
specify it. There's also no need to check for a match before doing the
substitution.
The following should do the trick:
#!/usr/bin/perl -w
use strict;
foreach (<DATA>) {
s/\b[Bb]ij\b ?//g;
print;
}
__DATA__
bij de bakker en niet bij de kapper
The output is:
de bakker en niet de kapper
Which has also got rid of the leading space in this case. (This won't
handle spaces correctly in cases like "xxx bij.", though, but it's no
worse than the original.) So
> if ($_ =~ /(^| |\.)(B|b)ij(\n| |\.)/) {
> s/(^| |\.)(B|b)ij(\n| |\.)/ /g;
> }
has become
s/\b[Bb]ij\b ?//g
Of course, your specific requirements may cause this to change. If you
want true case-insensitivity (at a performance cost, I believe), you
can dispense with "[Bb]" in favour of "b" and "/i". If spacing is
important, you should either capture the spaces on either side, and
depending on what is captured, substitute a space or not; or treat
each spacing case separately.
---
> One other problem appears when I want to substitute a
> multi-word expression and the words are spread accross lines.
> E.g.
>
> while (<>) {
> if ($_ =~ /(^| |\.)(I|i)n(\n| )verband(\n| )met(\n| |\.)/) }
> s/(^| |\.)(I|i)n(\n| )verband(\n| )met(\n| |\.)/ /g;
> }
> }
>
> This does not delete the regexp from the text, which is what I want to do.
> If two consecutive lines say:
>
> hij zei iets in verband
> met het studentenblad
>
> then nothing happens. Can anyone help me?
There's an FAQ on this too (are you starting to get the hint?):
"I'm having trouble matching over more than one line. What's
wrong?"
As well as the FAQs, you should read the other manpages, especially
perlre and perlop in this case.
Regards,
Ian
------------------------------
Date: Wed, 10 Oct 2001 00:45:21 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Signaling
Message-Id: <3BC3D261.7992091E@earthlink.net>
plambe wrote:
>
> Hi all,
>
> I have the following problem:
> I work under WinNT40 and I want to use Signals to glue together a
> script using external executables. $
This an example of an XY problem.
"I want to do X. I think I have to do Y to do X. How do I do Y?"
> I have an executable (hvite.exe) which I want to run from perl script
> and then at given moment to send a signal to it from the perl code.
>
> This executable is configured 5 seconds after running to wait for a
> given signal (lets say INT).
"is configured?" Does that mean that someone else wrote it, and you
can't change it [ie someone configured it that way], or are you saying
that you can configure it to do /whatever/.
> When it receives the signal it should start recording
> live audio then on receiving a second signal should stop recording.
>
> My idea was to open the executable like pipe for writing. Then sleep
> for 5 secs and signal it. Then another 2 sec and signal it.
Signaling doesn't work in windows.
Instead of attempting to send a signal, how about you use that handy
pipe which you've already opened?
When you want the proggy to start, write "start\n", and when you want it
to stop, write "stop\n".
Of course there is one minor teensy weensy problem: when you open a
program as a pipe to read to or write from, perl on windows only
*pretends* to fork and run it asynchronously... it doesn't really it.
When you do:
open( FOO, "|baz.exe" );
print FOO "foo!\n";
close FOO;
It is *really* doing something like:
open( FOO, ">temp" );
print FOO "foo!\n";
close FOO; system("baz.exe < temp"); unlink("temp");
And when you do:
open( FOO, "baz.exe|" );
@x = <FOO>;
close FOO;
It is *really* doing something like:
system("baz.exe > temp"); open( FOO, "<temp" );
@x = <FOO>;
close FOO; unlink("temp");
To get a true asynchronous process on windows, use IPC::Open{2,3}.
use IPC::Open2;
my $pid = open2(FOO, ">&STDOUT", "hvite.exe");
sleep 5;
print FOO "start\n";
sleep 2;
print FOO "stop\n";
close FOO;
if( waitpid( $pid, 0 ) ) {
my ($sig, $ret) = ($?&255, $?>>8);
die "hvite died from signal $sig" if $sig;
die "hvite exited with code $ret" if $ret;
} else {
die "waitpid($pid,0): $!";
}
--
"Just how stupid are you Kuno?"
"Verily, Tatewaki Kuno knows no limits."
------------------------------
Date: 9 Oct 2001 22:07:26 -0700
From: fridden@yahoo.com (Fredrik)
Subject: Re: sort of array containing strings doesn't work
Message-Id: <9ef98ad9.0110092107.4872b792@posting.google.com>
>> rjocham72@netscape.net (Ralph Jocham) wrote in message
> Then I sort the array with : sort @uniq;
> But the result is not sorted. Is there a size limite. The array contains
> about 2000 lines??
> Here a sniplet of the last couple of lines:
There should be no problem to sort a 2000 entries long array.
From your posting it seems like you think the statement "sort @uniq;"
would modify the @uniq array and it doesnt. You must assign the
resulting list to something.
@sorted = sort @uniq;
or
@uniq = sort @uniq; #
If this is indeed your problem you should note that the use warnings
pragma can help you detect this kind of error or mistake.
Hope this helps
Fredrik.
------------------------------
Date: Wed, 10 Oct 2001 02:11:08 GMT
From: "jzh" <jzh@earthlink.net>
Subject: Sort unique Very large file
Message-Id: <07Ow7.3806$7B1.150521@newsread2.prod.itd.earthlink.net>
I have a very large file over 3million records "<FH> in example below" with
nonunique records.
I am looking to sort without putting the contents of the file into an array
or hash.
I would like to do a very low level operation line by line not to put any
strain on memory limits.
My code is this:
# Get unique key
#
sub get_key_sub {
open (FH, "$file") or die "Err File open $file";
open (FHKEY, ">$filekey") or die "Err File open $filekey";
for (<FH>) {
if ($_=~/^S/) {$unique{$_}=$_ }
}
for (sort keys %unique) {
chomp ($_);
print FHKEY "$_\n";
}
close (FH);
close (FHKEY);
}
This exert reads in <FH> and prints out the unique values in <FHKEY>
Thanks in advance for your help
Joey
------------------------------
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 1901
***************************************