[18984] in Perl-Users-Digest
Perl-Users Digest, Issue: 1179 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 23 03:05:40 2001
Date: Sat, 23 Jun 2001 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)
Message-Id: <993279908-v10-i1179@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 23 Jun 2001 Volume: 10 Number: 1179
Today's topics:
Re: BCD Binary Coded Decimal Problem!! <goldbb2@earthlink.net>
Re: Can't install CPAN module <tony_curtis32@yahoo.com>
Re: command line news posting tools <poepping@ksni.net>
Re: convert letters to numbers <goldbb2@earthlink.net>
Re: convert letters to numbers <joe+usenet@sunstarsys.com>
Error installing CSV_XS <sforshee@umr.edu>
Re: How do I use module if I don't have the permission <somewhere@in.paradise.net>
Re: How to check for open files on NT <kevinsproule@hotmail.com>
Re: http://stein.cshl.org/WWW/software/CGI/#dos <goldbb2@earthlink.net>
Re: Perl Programmer needed (David H. Adler)
perl Tk::DragDrop (comp.lang.perl.tk has not responded) <person@company.com>
Re: perl Tk::DragDrop (comp.lang.perl.tk has not respon <goldbb2@earthlink.net>
Re: Possible to split GIF or JPG into 12 images? (Mark Jason Dominus)
Re: Problem reading large files (dumb question?) (Mark Jason Dominus)
Redirection and name=value <abuse@SCasey.com>
Re: Redirection and name=value <tony_curtis32@yahoo.com>
Re: regular expression problem <ren@tivoli.com>
Re: simple reference question (Mark Jason Dominus)
Re: what's the Perl Code for removing 1st two character (Craig Berry)
Re: what's the Perl Code for removing 1st two character (Tony L. Svanstrom)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 23 Jun 2001 02:22:42 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: BCD Binary Coded Decimal Problem!!
Message-Id: <3B3435B2.477297E4@earthlink.net>
Harry Yau wrote:
[snip]
> $0.05126 * 2 = $0.10252
> supposingly, but the result will become something like
> 0.102519999999997986.
> I think this is the problem of my CPU or the complier that they
> can't represent the decimal number precisely in binary mode
> underneath. Althougth I can round off the result to get a pretty good
> answer for each part, the cost of the final product
> becomes unacceptable after summing up the price of all litte parts.
"Doctor, it hurts when I do this." "Then don't *do* that."
Don't round off at each part, only at the end of all your computations.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: 22 Jun 2001 17:06:11 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Can't install CPAN module
Message-Id: <87itho5fvw.fsf@limey.hpcc.uh.edu>
>> On 22 Jun 2001 21:49:06 GMT,
>> Kurt Tappe <ktappe@assocgraphics.com> said:
> Newbie question: I'm trying to install the CPAN module
> Net::FTP for use with my Perl scripting projects. But
> I've hit an odd problem.
perl -MCPAN -eshell
makes life a whole lot easier...
> # perl Makefile.PL Writing Makefile for
> Net::FTP::Common # make bash: make: command not found #
> ./make bash: ./make: No such file or directory
> How is it I can't find make when I'm root? This is
> driving me more than a bit nuts.
Compare the PATH variables for you and root.
hth
t
--
Just reach into these holes. I use a carrot.
------------------------------
Date: Fri, 22 Jun 2001 19:03:03 -0600
From: Matt Poepping <poepping@ksni.net>
Subject: Re: command line news posting tools
Message-Id: <wRRY6.2097$G92.605690@news.uswest.net>
inews I think will work.
Alexis Huxley wrote:
> In <sa8n172wnck.fsf@suntong.personal.users.sourceforge.net> * Tong *
> <sun_tong@users.sourceforge.net> writes:
>
>> Please suggest a news posting tool that can be used from command
>> line, which has the function that like 'sendmail -t' (scanned
>> message for headers) TIA
>
> Try nnpost, which is part of the 'nn', still the king of NUA's?:
>
> te35$ nnpost -s "test alexis" -k "" -y "" -d world -f /dev/null -p
> de.test Connecting to NNTP server news.hq.eso.org ...
> Connecting to NNTP server news.hq.eso.org ... ok (posting is allowed)
> Be patient! Your new article will not show up immediately.
> Article posted
> te35$
>
> Alexis
> --
>
------------------------------------------------------------------------------
> Alexis Huxley
> ahuxley@eso.org
> Rilke,SW,me
> http://www.eso.org/~ahuxley/
>
------------------------------------------------------------------------------
------------------------------
Date: Fri, 22 Jun 2001 18:30:35 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: convert letters to numbers
Message-Id: <3B33C70B.DC428E51@earthlink.net>
Bart Lateur wrote:
>
> Abigail wrote:
>
> > sub str2number {
> > my $str = shift;
> > $cache {$str} = ++ $counter unless $cache {$str};
> > $cache {$str}
> > }
>
> sub str2number {
> my $str = shift;
> $cache{$str} ||= ++$counter;
> }
>
> or
>
> sub str2number {
> my $str = shift;
> $cache{$str} ||= keys %cache;
> }
And what happens if somebody deletes a key from %cache? You'll lose
your property of uniqueness.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: 22 Jun 2001 18:30:15 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: convert letters to numbers
Message-Id: <m3bsng2lmw.fsf@mumonkan.sunstarsys.com>
Benjamin Goldberg <goldbb2@earthlink.net> writes:
> Bart Lateur wrote:
> >
> > Abigail wrote:
> >
> > > sub str2number {
> > > my $str = shift;
> > > $cache {$str} = ++ $counter unless $cache {$str};
> > > $cache {$str}
> > > }
> >
> > sub str2number {
> > my $str = shift;
> > $cache{$str} ||= ++$counter;
> > }
> >
> > or
> >
> > sub str2number {
> > my $str = shift;
> > $cache{$str} ||= keys %cache;
> > }
>
> And what happens if somebody deletes a key from %cache? You'll lose
> your property of uniqueness.
Or just resets $counter? But if you look at the full text of Abigail's
original suggestion, I think you'd see (ignoring the typo) that these
points were already fully addressed.
--
Joe Schaefer "The eternal mystery of the world is its comprehensibility."
--Albert Einstein
------------------------------
Date: Fri, 22 Jun 2001 19:05:25 -0500
From: "Seth Forshee" <sforshee@umr.edu>
Subject: Error installing CSV_XS
Message-Id: <m1RY6.4366$Ib.468722@news1.primary.net>
I'm having trouble installing the CPAN module Text::CSV_XS. I'm installing
into a subdirectory of my home directory because I don't have privilages to
install into the systems repository of Perl modules. When I enter make, I
get the following error:
cpp: Cannot reallocate 4128 bytes after allocating 16772152 bytes
*** Error exit code 1
What can I do to install the module?
------------------------------
Date: Sat, 23 Jun 2001 10:57:20 +1000
From: "Tintin" <somewhere@in.paradise.net>
Subject: Re: How do I use module if I don't have the permission to install?
Message-Id: <yQRY6.1$%q1.88151@news.interact.net.au>
"chih-chieh, Huang" <h155@ms37.hinet.net> wrote in message
news:a488b3e.0106220604.793c56bd@posting.google.com...
> Dear All:
>
> I don't have the permission to install the module I need on the server I
use.
> And, it's not possible the administrator will give me that permission.
>
> If there any possibility that I can still use module which is not
currently
> installed?
Yes. See FAQ
perldoc -q module
------------------------------
Date: Sat, 23 Jun 2001 05:14:18 GMT
From: "Kevin M. Sproule" <kevinsproule@hotmail.com>
Subject: Re: How to check for open files on NT
Message-Id: <KAVY6.99609$L4.11704750@news1.rdc1.az.home.com>
"Mark Law" <marklaw@mediaone.net> wrote in message
news:lNlY6.5153$bK1.93930@typhoon.mw.mediaone.net...
> I'm writing a store and forward FTP application to pick up files in NT
> directories and move them from a staging area. The files are deposited at
> random times, and some are quite large, >6GB. I obviously don't want to
> start the FTP tranfer of the files until they have been completely written
> to the staging area. Since the file directory entry is created at the
time
> the transfer begins to the staging area, I don't know how to test when the
> transfer to the staging area is complete. I've looked at
Lockfile::Simple,
> Win32::File and Win32API::File, Win32::NetAdmin, Win32::NetResource,
> Win32::Process, Win32API::Net. None seem to be able to tell when a file
is
> "closed" for writing by other processes.
>
> I would like to do something simple like:
> if ( ! $fileopen =
> SOME_UNKNOWN_PACKAGE::some_unknown_method($filename) ) {
> ....don't process file, some other process has it open for
> writing
> } else {
> ....begin FTP process
> }
>
> Has anyone run across this before?
>
>
Mark,
Here is a sub that I tested under WIN-98. You might give it a try under NT
to see if it behaves the same. Good luck.
Kevin Sproule
#!/perl/bin/perl.exe -w
use strict;
sub fileopen($) { # detects if a file is open on WIN-32
my $filename = shift;
my $toname = $filename."X";
my $opened;
local *IFILE;
if (! -e $filename) { # the file does not exist
return 0;
}
rename $filename, $toname; # this just fails without warning on WIN-32
$opened = open(IFILE,"< $toname");
if ($opened) {
close(IFILE);
rename $toname, $filename;
return 0;
}
return 1;
}
if (fileopen("test.txt")) {
print "The file is open\n";
} else {
print "The file is closed\n";
}
------------------------------
Date: Fri, 22 Jun 2001 18:24:49 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: http://stein.cshl.org/WWW/software/CGI/#dos
Message-Id: <3B33C5B1.170F1B21@earthlink.net>
Chris wrote:
>
> in article tj52ovosmj4ufe@corp.supernews.co.uk, David Soming at
> davsoming@lineone.net wrote on 06/21/2001 7:09 PM:
>
> > I'm wanting to use this to limit posts to 30 characters for email
> > addresses. Would CGI::POST_MAX be the solution or is 100K the
> > minimum?
>
> To limit an HTML field to a length, you should code it into the tag,
> as:
>
> <input type='text' maxlength='30' name='email' size='15'/>
>
> Size is the size of the field when displayed in the browser. Maxlength
> is the maximum number of characters that a user can enter.
Yes, but don't forget, just because you tell the browser to limit the
number of characters to send, doesn't mean that it necessarily will.
You should both include a size parameter to the input tag, AND have your
script test that the amount sent is less than your limit.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: 23 Jun 2001 05:19:37 GMT
From: dha@panix.com (David H. Adler)
Subject: Re: Perl Programmer needed
Message-Id: <slrn9j89n9.kkm.dha@panix2.panix.com>
In article <4cd600b4.0106221055.2114ba44@posting.google.com>, missrockieh wrote:
> I'm looking for a PERL programmer
You have posted a job posting or a resume in a technical group.
Longstanding Usenet tradition dictates that such postings go into
groups with names that contain "jobs", like "misc.jobs.offered", not
technical discussion groups like the ones to which you posted.
Had you read and understood the Usenet user manual posted frequently to
"news.announce.newusers", you might have already known this. :) (If
n.a.n is quieter than it should be, the relevent FAQs are available at
http://www.faqs.org/faqs/by-newsgroup/news/news.announce.newusers.html)
Another good source of information on how Usenet functions is
news.newusers.questions (information from which is also available at
http://www.geocities.com/nnqweb/).
Please do not explain your posting by saying "but I saw other job
postings here". Just because one person jumps off a bridge, doesn't
mean everyone does. Those postings are also in error, and I've
probably already notified them as well.
If you have questions about this policy, take it up with the news
administrators in the newsgroup news.admin.misc.
http://jobs.perl.org may be of more use to you
Yours for a better usenet,
dha
--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
I expect my version of ESP::Psychic will be ready sometime last week.
More likely, yesterday (you know how schedules tend to slip).
- Eric The Read in comp.lang.perl.misc
------------------------------
Date: Fri, 22 Jun 2001 15:21:47 -0700
From: The Small Kahuna <person@company.com>
Subject: perl Tk::DragDrop (comp.lang.perl.tk has not responded)
Message-Id: <3B33C4FB.2755E65D@company.com>
Yes, I know about comp.lang.perl.tk but I posted this a few days ago and
the only response I got was "yep, happens to me too". Its still driving
me crazy.
-------
I'm going nuts with this one.
I'm on HPUX and using 5.004_04 (yes, yes, I know, but I get the same
result on 5.005) and my window manager is fvwm2.
Problem:
I have a toplevel with a text widget in it. DragDrop works perfectly if
I keep the window the same size as it used to be.
HOWEVER!
If I resize the window (larger) while I drag the item to the
destination, it just will *not* drop in the new location, but what is
REALLY wierd is that it *will* drop in the space it WOULD HAVE BEEN IN
before I resized the window. (In other words, if I drag the item to the
same coordinates the widget used to be at (relative to the top left
corner of the toplevel), it works fine.
IF!
I then switch to another desktop, when I switch back, it works fine. I
overloaded Tk::DragDrop::Rect::Over to add some instrumentation code and
what is happening is that the call to $x = $site->X; is not reflecting
the new true X value until I switch desktops back and forth.
My neurons are leaking out of my ears.
Obviously, fvwm2 is doing *something* to the window when it commands it
to redraw that changes the X,Y values, BUT WHAT and how do I do the same
thing when the window gets resized?
My demo program is as follows. To run it, invoke it and drag an
environment variable to the entry widget. Make the window much bigger.
Drag a new environment variable to where the entry widget is NOW. Note
how it does not work right. Drag the environment variable to where the
entry widget USED TO BE. Notice it works.
AARRGGGGHHHHHHH.
use Tk;
use Tk::DragDrop;
use Tk::DropSite;
use strict;
use vars qw($top $f $lb_src $lb_dest $dnd_token $top1 $f1);
$top = new MainWindow;
$top->Label(-text => "Drag items from the left listbox to the right one"
)->pack;
$f = $top->Frame->pack;
$lb_src = $f->Scrolled('Listbox', -scrollbars => "osoe",
-selectmode=>'extended')
->pack(-side => "left");
$top1 = new MainWindow;
my $text=$top1->Scrolled('Text')->pack(-side=>'top',-expand=>1,
-fill=>'both');
$f1 = $top1->Frame->pack;
$lb_dest = $f1->Scrolled('Entry', -scrollbars => "osoe")
->pack(-side => "left");
$lb_src->insert("end", sort keys %ENV);
# Define the source for drags.
# Drags are started while pressing the left mouse button and moving the
# mouse. Then the StartDrag callback is executed.
$dnd_token = $lb_src->DragDrop
(-event => '<B1-Motion>',
-sitetypes => ['Local'],
-startcommand => sub { StartDrag($dnd_token) },
);
# Define the target for drops.
$lb_dest->DropSite
(-droptypes => ['Local'],
-dropcommand => [ \&Drop, $lb_dest, $dnd_token ],
);
MainLoop;
sub StartDrag {
my($token) = @_;
my $w = $token->parent; # $w is the source listbox
my $e = $w->XEvent;
my $idx = $w->nearest($e->y); # get the listbox entry under cursor
if (defined $idx) {
# Configure the dnd token to show the listbox entry
$token->configure(-text => $w->get($idx));
# Show the token
my($X, $Y) = ($e->X, $e->Y);
$token->MoveToplevelWindow($X, $Y);
$token->raise;
$token->deiconify;
$token->FindSite($X, $Y, $e);
}
}
# Accept a drop and insert a new item in the destination listbox.
sub Drop {
my($lb, $dnd_source) = @_;
$lb->insert("end", $dnd_source->cget(-text));
#$lb->see("end");
}
------------------------------
Date: Sat, 23 Jun 2001 02:07:37 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: perl Tk::DragDrop (comp.lang.perl.tk has not responded)
Message-Id: <3B343229.F1DD8298@earthlink.net>
While this has little or nothing to do with your problem, I'd like to
know why you use all global variables, not lexicals. Also, if you used
warnings, you would realize that $text is only assigned to, and never
used again. Also, your use of $f and $f1 seems odd... you only use the
immediately after they're assigned to, and never again. I would have
written the program as follows:
#! perl -w
use Tk;
use Tk::DragDrop;
use Tk::DropSite;
use strict;
my $dnd_token;
WINDOW_1 : {
my $top = MainWindow->new;
$top->Label(-text =>
"Drag items from the left listbox to the right one"
)->pack;
my $lb = $top->Frame->pack->Scrolled('Listbox',
-scrollbars => "osoe", -selectmode => 'extended'
)->pack(-side => "left");
$lb->insert("end", sort keys %ENV);
$dnd_token = $lb->DragDrop (
-event => '<B1-Motion>',
-sitetypes => ['Local'],
-startcommand => sub {
my $token = $dnd_token;
my $w = $token->parent; # $w is the source listbox
my $e = $w->XEvent;
my $idx = $w->nearest($e->y);
# get the listbox entry under cursor
return unless defined $idx;
# Configure the dnd token to show the listbox entry
$token->configure(-text => $w->get($idx));
# Show the token
my($X, $Y) = ($e->X, $e->Y);
$token->MoveToplevelWindow($X, $Y);
$token->raise;
$token->deiconify;
$token->FindSite($X, $Y, $e);
} );
} # end WINDOW_1 block.
WINDOW_2 : {
my $top = MainWindow->new;
$top->Scrolled('Text')->pack(
-side=>'top', -expand=>1, -fill=>'both');
my $lb = $top->Frame->pack->Scrolled('Entry',
-scrollbars => "osoe")->pack(-side => "left");
$lb->DropSite (
-droptypes => ['Local'],
-dropcommand => [ sub {
$_[0]->insert("end", $_[1]->cget(-text));
}, $lb, $dnd_token ] );
} # end WINDOW_2 block.
MainLoop;
__END__
Note that this code is untested.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Sat, 23 Jun 2001 02:17:40 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Possible to split GIF or JPG into 12 images?
Message-Id: <3b33fc29.c24$225@news.op.net>
In article <Xns90C88863F1D96dragnetinternalysisc@207.35.177.135>,
Marc Bissonnette <dragnet@internalysis.com> wrote:
>I am wondering if it is possible to use Perl to split an image into 9 (or
>whatever number you wanted) separate images?
I ususally use 'pnmcut' for jobs like that. It's free software and
easy to script.
--
@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: Fri, 22 Jun 2001 22:01:31 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Problem reading large files (dumb question?)
Message-Id: <3b33c03a.4c1$45@news.op.net>
In article <9h0abu$55t$1@cnn.Princeton.EDU>,
Christopher J. Mackie <cjmackie@princeton.edu> wrote:
>Apologies if this is a CompSci 101 question
>Is this there something different
>about files over 2Gb, or am I just doing something stupid?
Nothing stupid. There is a backward-compatibility problem on Unix
systems that support large files. The (C library) functions that read
data, such as 'read()', take 'int' parameters. But typical 'int's are
only 32 bits long, and can't accomodate numbers larger than 2147483647.
You need to obtain the Perl source code and rebuild Perl with the
'large file' options enabled. Rerunning the 'Configure' program and
answering the questions about 'large files' will probably produce a
Perl that works with large files. If you do this, your program will
probably run without change. Also consult the README file for your
system.
--
@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: Sat, 23 Jun 2001 01:00:35 GMT
From: "Sean Casey" <abuse@SCasey.com>
Subject: Redirection and name=value
Message-Id: <TSRY6.98460$L4.11564834@news1.rdc1.az.home.com>
Hello,
I've an application that has a requirement to
redirect/recall/run a script on another server (a shopping cart), passing to
it several form parameters via method POST. I have managed to locate the
redirect function provided by GGI.pm, but can find no way to also send along
the parameters.
In early development, my shopping cart 'simulator' was another script in my
cgi-bin, so:
$shopprog = "./otherscript.pl";
open (SHOPPER, "| $shopprog " ) or die "Cannot start shopping program.
$!\n";
print SHOPPER "Filename=$filename\&";
print SHOPPER "Question=$question\&";
print SHOPPER "Email=$email\n";
close(SHOPPER);
worked just fine - except that it didn't 'redirect' the browser. That
seemed not to matter, although it probably would in the real world.
However, when $shopprog = "http://www.someotherdomain.com/some.html";
I (naturally) got the error "cannot exec ... no such file..."
So, I used redirect, but I have not found a way to include the name=value
pairs.
As of now, the script writes out a web page with a
form containing the type=hidden input tags and a JavaScript:
<body onLoad="document.forms[0].submit();">
which works, but overwhelms me with it's geekyness.
It also prevents the use of the brower's Back.
If the user goes Back, the onLoad submit() just kicks them forward again.
Any suggestions or pointers would be greatly appreciated.
TIA,
Sean
RH 6.0 and 6.2
perl 5.005_03 and 5.004_04 respectively
------------------------------
Date: 22 Jun 2001 20:08:57 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Redirection and name=value
Message-Id: <87bsng57fa.fsf@limey.hpcc.uh.edu>
>> On Sat, 23 Jun 2001 01:00:35 GMT,
>> "Sean Casey" <abuse@SCasey.com> said:
> Hello, I've an application that has a requirement to
> redirect/recall/run a script on another server (a
> shopping cart), passing to it several form parameters
> via method POST. I have managed to locate the redirect
> function provided by GGI.pm, but can find no way to also
> send along the parameters.
You can't. POST requests do not put their contents in the
URL. You can do this with GET though, which is what the
browser (or whatever is at the client side) will do with
the headers returned via the redirect() method.
You'll need to use URI (see query_form method) and LWP
(LWP::UserAgent, HTTP::Request) instead to construct and
send the request.
perldoc lwpcook
hth
t
--
Just reach into these holes. I use a carrot.
------------------------------
Date: 22 Jun 2001 17:56:45 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: regular expression problem
Message-Id: <m3ofrg9l8y.fsf@dhcp9-173.support.tivoli.com>
On 22 Jun 2001, lxl22@visto.com wrote:
> jacklam@math.uio.no (Peter J. Acklam) wrote in message
> news:<cxclmmk388v.fsf@masterblaster.uio.no>...
>> lxl22@visto.com (Xtreme) wrote:
>>
>> > Assuming I have a string of the form "a_b_b_c" in which I don't
>> > know how many "b_" substrings it has, how can I, in one single
>> > s/// line replace every b_ for a "x_"? My main problem is that
>> > I don't know how to tell the re engine how to replace it the
>> > same number of times as it occurred.
>>
>> s/b_/x_/g;
>
> Thanks, Peter. I had forgotten to mention that a,b and c can each be
> distinct or the same -- I don't know ahead of time, and that I don't
> want to replace a or c (i.e. not the first or last in the string). I
> figured it out in the mean time by going through (of all things) the
> faq:
> s/\G_b_/_x_/g;
> I still don't really grasp the \G, but it works!
Wow.
I'm not sure what's going on there, as \G does not appear to have a
defined behavior in a global substitute. And the behavior it exhibits
certainly isn't what I would expect. In fact, the behavior I see
doesn't achieve your stated goal:
perl <<'__END__'
$_ = "b_b_b_b_b_b";
s/\G_b_/_x_/g;
print "$_\n";
__END__
b_b_b_b_b_b
Skipping the whole \G issue, here is a solution that I believe does
achieve your stated goal:
perl <<'__END__'
$_ = "b_b_b_b_b_b";
s/(?<=_)b(?=_)/x/g;
print "$_\n";
__END__
b_x_x_x_x_b
It actually goes a bit overboard on the advanced regex features, but
I'm a bit partial to only replacing the part you actually want to
change. It just feels more maintainable. If at some point you change
to "a-b-b-b-b-c", then you only have to change the match portion,
which you would expect to have to change. Removing the unnecessary
dependency in the replacement string seems like a good thing.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: Fri, 22 Jun 2001 22:12:38 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: simple reference question
Message-Id: <3b33c2d6.51e$2bb@news.op.net>
In article <rob1jtc5qnpg6dnbgaroo4rj0f06b5c752@4ax.com>,
Philip Newton <nospam.newton@gmx.li> wrote:
>Also, use requires a bareword. You might be able to use something like
>
> require "$input.pm";
> import $input;
This won't work if $input contains a '::'. To be completely general,
you need something like this:
$input =~ s{::}{/}g;
$input .= ".pm";
eval { require $input && $input->import() }
if ($@) { it didn't work }
--
@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: Fri, 22 Jun 2001 22:10:44 -0000
From: cberry@cinenet.net (Craig Berry)
Subject: Re: what's the Perl Code for removing 1st two characters of a string?
Message-Id: <tj7gj4l0612d4e@corp.supernews.com>
I've trimmed clp.modules from this post and from followups, as it's not a
module-related issue.
Please choose a shorter subject (say, "Removing characters" in this case),
and put your full question in the body of your message. This makes it
easier to read your question as a unit, and to quote the question in
replies if relevant. I'll do so for you, as the first quoted line below.
Daniel Czajko (czajko@ocas.on.ca) wrote:
: what's the Perl Code for removing 1st two characters of a string?
Removing a fixed number of characters anywhere in a string is best done
using substr() in its 'lvalue' (assignable) role. See 'perldoc -f substr'.
: Actually, I need the code for removing:
: -- 1st two characters
substr($str, 0, 2) = '';
: -- 1st three character
substr($str, 0, 3) = '';
: -- last two characters
substr($str, -2) = '';
--
| Craig Berry - http://www.cinenet.net/~cberry/
--*-- "Magick is the art and science of causing change in conformity
| with Will." - Aleister Crowley
------------------------------
Date: Sat, 23 Jun 2001 04:45:30 GMT
From: tony@svanstrom.com (Tony L. Svanstrom)
Subject: Re: what's the Perl Code for removing 1st two characters of a string?
Message-Id: <1evfzyi.11e856i1olhhtsN%tony@svanstrom.com>
Daniel Czajko <czajko@ocas.on.ca> wrote:
> Actually, I need the code for removing:
>
> -- 1st two characters
> -- 1st three character
> -- last two characters
Any character is ze lovely dot, and the rooftop means the begining of ze
text and the dollar the end...
$line =~ s/^(.)//;
$line =~ s/(.)$//;
I'm sure you can take it from there and make it remove as many
characters as you need.
/Tony
--
########################################################################
I'm sorry, I'm sorry; actually, what I said was:
HOW WOULD YOU LIKE TO SUCK MY BALLS?
- South Park -
------------------------------
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 1179
***************************************