[8953] in Perl-Users-Digest
Perl-Users Digest, Issue: 2571 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 12 17:10:00 1998
Date: Tue, 12 May 98 14:01:48 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 12 May 1998 Volume: 8 Number: 2571
Today's topics:
Re: regexp for strings of chars (Craig Berry)
Re: regexp for strings of chars (Mike Stok)
Re: regexp for strings of chars <tchrist@mox.perl.com>
Re: regexp for strings of chars <rootbeer@teleport.com>
Re: removing nested parentheses (Petr Prikryl)
Re: removing nested parentheses (Petr Prikryl)
Re: Scanning Perl scripts for Y2K problems (Bart Lateur)
Re: Significant digit? (Craig Berry)
Re: Significant digit? <yong@shell.com>
Re: Significant digit? (John Stanley)
Re: sourcing a file in Perl <yong@shell.com>
Re: sourcing a file in Perl <yong@shell.com>
Re: Tip: Filehandles <tchrist@mox.perl.com>
Tk::Balloon and warning message? (Wayne C. McCullough)
Re: What happenned to all the 1's. <rootbeer@teleport.com>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 12 May 1998 19:54:45 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: regexp for strings of chars
Message-Id: <6ja9e5$8p7$2@marina.cinenet.net>
Lee M Horowitz (lhorow@corp.idt.net) wrote:
: I cant help thinking there's a "neat" way to do this, but somehow I cant
: think of it...
: I'ld like a regexp that matches a string containing a "run" of lets say
: THREE of the SAME
: letter or number. Any letter or number would do. In other words xaaabcds
: would match
: but so would abcxxx or abc111qzd or .... you get it.
First, you might wish to configure your newsreader to wrap your columns
at 78 characters, or switch to another newsreader if this isn't possible
on the one you have.
Now, here are some possibilities, which we'll match using /g in a list
context to see what pops up from this test string:
$_ = 'aaaaaabbb,,,xyfooffff';
@a = m/.{3}/g; # ('aaa', 'aaa', 'bbb', ',,,', 'fff')
@b = m/\w{3}/g; # ('aaa', 'aaa', 'bbb', 'fff')
@c = m/.{3,}/g; # ('aaaaaa', 'bbb', ',,,', 'ffff')
@d = m/\w{3,}/g; # ('aaaaaa', 'bbb', 'ffff')
There are lots of other possibilities, but this should convey the basic
idea. Hope this helps!
---------------------------------------------------------------------
| Craig Berry - cberry@cinenet.net
--*-- Home Page: http://www.cinenet.net/users/cberry/home.html
| Member of The HTML Writers Guild: http://www.hwg.org/
"Every man and every woman is a star."
------------------------------
Date: 12 May 1998 20:09:33 GMT
From: mike@stok.co.uk (Mike Stok)
Subject: Re: regexp for strings of chars
Message-Id: <6jaa9t$mp9@news-central.tiac.net>
In article <3558A008.13552D3E@corp.idt.net>,
Lee M Horowitz <lhorow@corp.idt.net> wrote:
>I cant help thinking there's a "neat" way to do this, but somehow I cant
>think of it...
>I'ld like a regexp that matches a string containing a "run" of lets say
>THREE of the SAME
>letter or number. Any letter or number would do. In other words xaaabcds
>would match
>but so would abcxxx or abc111qzd or .... you get it.
You might consider backreferences (which make perl's regexes not regular)
e.g.
if ($string =~ /(.)\1{2}/) {
...
}
You should consult the perlre man page to find out more about how perl's
regexes work (are \1 and $1 different? what does . match? etc. :-).
Hope this helps,
Mike
--
mike@stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/ | 65 F3 3F 1D 27 22 B7 41
stok@colltech.com | Collective Technologies (work)
------------------------------
Date: 12 May 1998 20:36:52 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: regexp for strings of chars
Message-Id: <6jabt4$lpr$7@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, Lee M Horowitz <lhorow@corp.idt.net> writes:
:I cant help thinking there's a "neat" way to do this, but somehow I cant
:think of it... <<<<BAD WRAP>>>
:I'ld like a regexp that matches a string containing a "run" of lets say
:THREE of the SAME <<<<BAD WRAP>>>
:letter or number. Any letter or number would do. In other words xaaabcds
:would match <<<<BAD WRAP>>>
:but so would abcxxx or abc111qzd or .... you get it.
I'm afraid that your newsreader has used MSFMH line-wrapping your posting.
Better check its settings.
--tom
--
"If Dennis Ritchie were the man who developed Modula-2 then C would be long forgotten."
--Tarjei Jensen
------------------------------
Date: Tue, 12 May 1998 20:41:02 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Lee M Horowitz <lhorow@corp.idt.net>
Subject: Re: regexp for strings of chars
Message-Id: <Pine.GSO.3.96.980512133943.21974O-100000@user2.teleport.com>
On Tue, 12 May 1998, Lee M Horowitz wrote:
> I'ld like a regexp that matches a string containing a "run" of lets say
> THREE of the SAME letter or number. Any letter or number would do.
I think you want a backreference, which is one of those things that looks
like '\8' in a pattern. What have you tried that didn't work?
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 12 May 1998 20:16:23 GMT
From: prikryl@dcse.fee.vutbr.cz (Petr Prikryl)
Subject: Re: removing nested parentheses
Message-Id: <6jaamn$qtb$1@boco.fee.vutbr.cz>
Solution proposed below.
M.J.T. Guy (mjtg@cus.cam.ac.uk) wrote:
>Ronald J Kimball <rjk@coos.dartmouth.edu> wrote:
>>Ala Qumsieh wrote:
>>> Doug E Fresh wrote:
>>> > [...]
>>> > $string = "2*(3+(4+5))"
>>>
>>> Hmmm ... I suggest you take a closer look at the greediness of matching
>>> operators!
>>> After you do that, you can try something like this:
>>> $string =~ s/.*\((.*)\)?/&process($1)/e;
>>
>>That will match '(4+5))' in the above string, and set $1 to '4+5))'. How
>>would that be useful, exactly?
>Actually, it's worse than that. Because of the spurious initial .*
>it actually matches the whole string. I expect Ala meant to say
> $string =~ s/\((.*?)\)/&process($1)/e;
>where the key character is that `?', which says "find the shortest match"
>and therefore in particular one not containing an inner ')'.
>But that still isn't right. In the example, that would match `(3+(4+5)'
>and set $1 to `3+(4+'. You have to match an inner string which doesn't
>contain either `(' or `)'. So you want
> $string =~ s/\(([^()]*)\)/&process($1)/e;
>As a general rule, it's better to write explicitly what you don't want
>in your string rather than using non-greedy matching, which is both
>inefficient (sometimes) and error-prone.
If I understand well the question, Doug wanted something similar to this:
---------------------------------------------------------------------
#! /path/to/your/perl
require 5.004; # probably any 5.xxx is enough
use strict;
{
my $s = '1+(2)+2*(3+4*(2+3*(36-5*(1+1)+3))) + 3*(256-38*(2-3*(5+2)))';
# ... you can read it from somewhere
print "the original: $s\n"; # just to know what happens
$s = "($s)"; # add one pair of () for safety
while ( $s =~ s/\(([^(]+?)\)/&process($1)/e ) {
#
# where \( and \) are characters '(' and ')' from the string,
# inner '(' and ')' define $1 after matching,
# [^(]+? stands for any string which does not contain parentheses,
# i.e. the content of innermost parentheses (non-greedy)
# or greedy '[^()]+' could be used,
# 'e' stands for evaluation of the right side of s///, i.e.
# process() function is called and the result is used for
# replacement of the matched '(expr)'
print "$s\n"; # you can get rid of the print
}
if ( $s =~ /\(|\)/ ) { die "Badly written expression"; }
# ... because not all parenthese matched
print "result: $s\n";
}
sub process {
my $expr = shift; # cannot be empty, but to be sure...
if (!defined $expr) { die "Empty subexpression processed"; }
my $result;
eval "\$result = $expr"; # you can do your own kind of evaluation
die $@ if $@; # die if the expression cannot be evaluated
print "\tprocess($expr) returns $result\n"; # result of one step
return $result;
}
---------------------------------------------------------------------
Any comments? (Each program contains a bug ;-)
Petr
--
Petr Prikryl (prikryl@dcse.fee.vutbr.cz) http://www.fee.vutbr.cz/~prikryl/
TU of Brno, Dept. of Computer Sci. & Engineering; tel. +420-(0)5-7275 218
------------------------------
Date: 12 May 1998 20:37:59 GMT
From: prikryl@dcse.fee.vutbr.cz (Petr Prikryl)
Subject: Re: removing nested parentheses
Message-Id: <6jabv7$qtb$2@boco.fee.vutbr.cz>
Jamie Hoglund (jhoglund@mirage.skypoint.net) wrote:
>Doug E Fresh <dhaber@seas.upenn.edu> wrote:
>: Is it possible to create a search string that will find the most nested
>: parentheses in a string or do I have to use index, substr etc. etc.
[...]
>: $string = "2*(3+(4+5))"
>: $string =~ s/somthing/&process($1)/e
>: # where $1 = "4+5"
[...] >: Doug
>I have files that contain things like: &VAR{&FUN{Data}};
>Each &NAME{....}; does a particular thing, I wound up using a recursive
>function to goto the inside "function" expand it, and move outward. Here's
>a couple snips of it:
>sub lookup_function($func,$param){ #{{{
> .....
> $function = shift;
> $param = shift;
> .....
> $param =~ s/\&([A-Z]+)\{(.*)\}/&lookup_function($1,$2)/ge;
> .....
>}
Every recursive function can be converted to a cycle -- see my solution
in this thread elsewhere. Anyway, your solution is interesting.
[...]
>Jamie
--
Petr Prikryl (prikryl@dcse.fee.vutbr.cz) http://www.fee.vutbr.cz/~prikryl/
TU of Brno, Dept. of Computer Sci. & Engineering; tel. +420-(0)5-7275 218
------------------------------
Date: Tue, 12 May 1998 20:54:38 GMT
From: bart.mediamind@tornado.be (Bart Lateur)
Subject: Re: Scanning Perl scripts for Y2K problems
Message-Id: <3558b5fb.573087@news.tornado.be>
Randal Schwartz wrote:
>Stuart> Scanning for "localtime" might be a good start...
>
>Uh, *why*?
>
>localtime() itself is Y2K.
>
>What are YOU saying?
Isn't that obvious? Everytime a user looks like he's calculating a date
as string, the code desreves closer inspection for Y2K bugs.
localtime() Y2K compliant? Yeah, right. Calculating years since 1900 is
dubious, to say the least.
Bart.
------------------------------
Date: 12 May 1998 20:09:49 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: Significant digit?
Message-Id: <6jaaad$8p7$3@marina.cinenet.net>
Craig Berry (cberry@cinenet.net) wrote:
: my $abcissa = $val / $expFactor;
Thinko on my part, there -- meant 'mantissa'. Been a while since my
math-heavy days...
---------------------------------------------------------------------
| Craig Berry - cberry@cinenet.net
--*-- Home Page: http://www.cinenet.net/users/cberry/home.html
| Member of The HTML Writers Guild: http://www.hwg.org/
"Every man and every woman is a star."
------------------------------
Date: Tue, 12 May 1998 15:21:14 -0500
From: Yong Huang <yong@shell.com>
To: brian d foy <comdog@computerdog.com>
Subject: Re: Significant digit?
Message-Id: <3558AF3A.A79D7252@shell.com>
Brian, I'm a chemist (PhD from Texas A&M last year). That's why I posted the question!
Seriously, printf and sprintf won't do the job. It's probably better handled as a string using substr, index (rindex) or
even split. If we ignore rounding (which doesn't hurt much), we don't need int, ceil and floor (in POSIX) either. Anyway,
I expect to see your code soon. If you don't do it, I will, some time later.
Yong Huang
brian d foy wrote:
> In article <Pine.GSO.3.96.980511164241.21974b-100000@user2.teleport.com>, Tom Phoenix <rootbeer@teleport.com> posted:
>
> >On Mon, 11 May 1998, Yong Huang wrote:
> >
> >> Did somebody write code to extract significant digits out of a number?
> >
> >The FAQ talks about this. Hope this helps!
>
> actually, i think the problem is a different one than printing a number
> with the appropriate number of digits. for instance, the number 1234 to
> two significant digits is 1200. the conventional use of sprintf and
> friends won't help this. it looks like it's a problem in rounding, but
> it's a bit more involved.
>
> give me a little time to work on a solution - it's something chemists
> and such like to think about :)
>
> --
> brian d foy <comdog@computerdog.com>
> CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
> Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
> Perl Mongers <URL:http://www.pm.org>
------------------------------
Date: 12 May 1998 20:37:00 GMT
From: stanley@skyking.OCE.ORST.EDU (John Stanley)
Subject: Re: Significant digit?
Message-Id: <6jabtc$m1i$1@news.NERO.NET>
In article <35592028.60DE@comenius.ms.mff.cuni.cz>,
Jan Krynicky <jkry3025@comenius.ms.mff.cuni.cz> wrote:
>Yong Huang wrote:
>>
>> I don't want to reinvent the wheel. Did somebody write code to extract
>> significant digits out of a number? I want to say something like
>#!/usr/bin/perl
>@zk = (0, 0.2, 0.23, .238, 0.2345, 1234.2, 123456, 1.46500, 0.00234,
>0.001);
># =>(0, 0.2, 0.23, 0.23, 0.23 , 1200 , 120000, 1.4 , 0.0023 ,
>0.001);
There are two incorrect answers in your example. To two significant
digits, 0.238 is 0.24 (not 0.23) and 1.45600 is 1.5 (not 1.4).
Also, when your code is tested with the value 10., the (incorrect)
result is 10, while the correct answer is 10. (with the trailing decimal
point).
------------------------------
Date: Tue, 12 May 1998 14:58:33 -0500
From: Yong Huang <yong@shell.com>
To: Brian Wheeler <bdwheele@indiana.edu>
Subject: Re: sourcing a file in Perl
Message-Id: <3558A9E8.803CD522@shell.com>
I don't understand your answer. Bill Hess's probably using C shell and
wants to find a perl function equivalent to the UNIX command source
myscript.csh.
Here's my answer:
exec "/bin/csh", "myscript.csh";
Yong Huang
Brian Wheeler wrote:
> In article <e33Lu0bf9GA.328@upnetnews03>,
> "Bill Hess" <brhess@email.msn.com> writes:
> > How does one 'source' a file in Perl?
> > I have tried to use Shell(source); and that has not worked.
> >
> > Bill Hess
> > brhess@msn.com
> >
> >
>
> 'require' will do what you want (its in the docs...)
>
> Brian Wheeler
> bdwheele@indiana.edu
------------------------------
Date: Tue, 12 May 1998 15:06:13 -0500
From: Yong Huang <yong@shell.com>
Subject: Re: sourcing a file in Perl
Message-Id: <3558ABB5.77F0396A@shell.com>
I want to say a little more.
The statement exec "/bin/csh", "myscript.csh"; doesn't affect the current
process. It only affects the child process. I don't know how to change the
current process setting (e.g. environment variables).
Yong Huang
Yong Huang wrote:
> I don't understand your answer. Bill Hess's probably using C shell and
> wants to find a perl function equivalent to the UNIX command source
> myscript.csh.
>
> Here's my answer:
>
> exec "/bin/csh", "myscript.csh";
>
> Yong Huang
>
> Brian Wheeler wrote:
>
> > In article <e33Lu0bf9GA.328@upnetnews03>,
> > "Bill Hess" <brhess@email.msn.com> writes:
> > > How does one 'source' a file in Perl?
> > > I have tried to use Shell(source); and that has not worked.
> > >
> > > Bill Hess
> > > brhess@msn.com
> > >
> > >
> >
> > 'require' will do what you want (its in the docs...)
> >
> > Brian Wheeler
> > bdwheele@indiana.edu
------------------------------
Date: 12 May 1998 20:14:34 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Tip: Filehandles
Message-Id: <6jaaja$lpr$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, mgjv@comdyn.com.au (Martien Verbruggen) writes:
:That is a very old fashioned way to do it, and hardly anything worth
:drooling about.
"Old-fashioned"? Fashion smashin'. It's the proper and correct
way to do it. It's not old-fashioned. This object craze has
got to be stopped. If I have one more person tell me that
open is old-fashioned, I'm revoking their Perl licence. Larry
doesn't touch that crud, and there's really very little reason to.
:You do realise that there is very likely a very good reason why perlfaq5
:states the following?
It was written that way because Nate didn't understand filehandles very
well yet. He's been upgraded since.
Using those modules for filehandles is a poor idea. It doesn't solve
the <$fh[$i]> problem, and it doesn't solve the print $fh[$i] "data\n"
problem. It's just syntactic sugar, and not much of it.
IO::File is a really lame "cure" for astrophobia. It costs nearly 5,000
lines and 17 files. It is disgusting and insane to take a simple function
like this:
sub get_foo {
local *FH;
open(FH, '/foo/bar');
return *FH;
}
and then go and suck in a megamodule just because you're afraid of a star.
It is antiperl to do something that increases this simple 4-line function
a two of orders of magnitude. And as soon as you start muttering about
the ->print or ->getline methods, you blow your performance by as much
as an order of magnitude. Pure madness.
Stop making things hard that should be easier. These C++ craving is
the antiperl speaking to you. Get it behind thee.
--tom
--
Just don't compare it with a real language, or you'll be unhappy... :-)
--Larry Wall in <1992May12.190238.5667@netlabs.com>
------------------------------
Date: 12 May 1998 20:15:52 GMT
From: wayne@Glue.umd.edu (Wayne C. McCullough)
Subject: Tk::Balloon and warning message?
Message-Id: <6jaalo$1vo$1@hecate.umd.edu>
With the appended program, I get a warning message:
Use of uninitialized value at D:\PERL\lib\site/Tk/Balloon.pm line 91.
if I a) close the window with the "done 2" button, or b)
close the window with the "done 1" button, or c) press "done 1"
before "done 2".
Looking at the code, I think it inadaquately checks to see that
everything exists. Maybe other destructor code is being called
before the balloon detach code gets called.
Any thoughts?
#!/usr/local/bin/perl -w
use Tk;
use Tk::Balloon;
$t = MainWindow->new;
$t->Button(-text =>"done 1", command => sub{$t->destroy})->pack;
$t2=$t->Toplevel;
$t2->Button(-text =>"done 2", command => sub{$t2->destroy})->pack;
$sb = $t2->Label()->pack;
$help = $t->Balloon(-state => "status", -statusbar => $sb);
$help->attach($t2,-msg => "test");
MainLoop;
__END__
(ps is there a better newsgroup for TkPerl issues?)
Wayne McCullough
------------------------------
Date: Tue, 12 May 1998 20:54:59 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Leon Stepanian <info@purco.qc.ca>
Subject: Re: What happenned to all the 1's.
Message-Id: <Pine.GSO.3.96.980512135242.21974R-100000@user2.teleport.com>
On Tue, 12 May 1998, Leon Stepanian wrote:
> > > @mainpage=split(s/SCBDOPL/$FORM{accesscode}/, $_);
> The 1's are being removed from the value before they are replaced.
It may help you to realize that the first parameter to that split is the
return value from s///. The return value from a successful s/// is 1. Do
you see what's happening now?
It's normal to use a pattern, but not a substitution, as the first
argument to split. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.misc (and this Digest), send your
article to perl-users@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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 2571
**************************************