[6973] in Perl-Users-Digest
Perl-Users Digest, Issue: 598 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 11 00:07:25 1997
Date: Tue, 10 Jun 97 21:00:22 -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, 10 Jun 1997 Volume: 8 Number: 598
Today's topics:
$[ <neil@parkwaycc.co.uk>
[Q] Broadcasting to forked clients? (Jason A. Gibb)
[Q]. Replacing a carriage return <jon@atwww.com.au>
C type macros? (Eric Finley)
Re: crypt/decrypt in perl (Nathan V. Patwardhan)
Re: Does print() buffer output? (Michael Fuhr)
Re: Double Quote in replacement string <rootbeer@teleport.com>
Re: embeding perl in C++ (Ken Fox)
HTML organization chart generator <andreg@corp.sgi.com>
Re: htons() (Michael Fuhr)
MS-DOS port of the Perl programming language 13j5;l@jumbo.com
multiple white spaces (Tri Duy Tram)
Re: newbie question about the sort function and hashes (hakan keskin)
Re: Parsing Comma Delemited Text DataBase jmohr@grizzly.ccsd.k12.wy.us
Re: Parsing Comma Delemited Text DataBase <nzawawi@worldnet.att.net>
Re: Parsing Comma Delemited Text DataBase <nzawawi@worldnet.att.net>
Passwording (on UNIX) <jefpin@bergen.org>
Perl Compiler & Modules psyclone@twd.net
perl joke of the day ? (Rick Klement)
Re: Regexpert's assistance required. (Brendan O'Dea)
Re: Simple, but yet hard reg exp problem? (Abigail)
Re: tr problems with variables (hakan keskin)
WIN32 modules Wanted (Spiro Angeli)
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 09 Jun 1997 14:14:35 +0100
From: Neil <neil@parkwaycc.co.uk>
Subject: $[
Message-Id: <339C01BB.267E@parkwaycc.co.uk>
I have an old Perl 4 script which contains the line
$[ = $1;
which I can't compile under my version of Perl 5.
Is this something new to Perl 5, or
were the people who ported it to my system lazy?
(In case you're wondering,
this is a simple script which uses just one array,
but the subscripts are e.g. 12345..12358,
so i want to set $[ to 12345.
Currently I'm working around this by using a hash instead,
but I was worried about performance considerations.)
Please e-mail as I don't read this group.
Thanks,
Neil.
------------------------------
Date: Tue, 10 Jun 1997 17:27:06 -0500
From: jgibb@rrnet.com (Jason A. Gibb)
Subject: [Q] Broadcasting to forked clients?
Message-Id: <jgibb-1006971727060001@news>
Hello all!
I'm attempting to set up a chat server using Perl 5.03. I've got the
socket connection part down, and each client can connect and communicate
with the server, but when the server forks, each child is unaware of the
other processes. I can't figure out how to "broadcast" a single message
that every client will hear. (Yes, I have read perlipc, but I'm afraid my
lack of unix background is preventing me from seeing what to do.)
I'm guessing I might be able to set up an array of socket file handles to
each client, but that seems unnecessarily messy to me. Is there a better
way?
Here's what I have so far, modified from the standard socket stuff.
(Apologies for posting it all, but I don't want to miss anything... 8-)
--------------cut
#! /usr/local/bin/perl -Tw
use Socket;
$port = 4567; # hardcoded for now
$channels = 10; #this may change too
$SIG{CHLD} = sub { wait(); }; # stop those zombies
($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyport($port, 'tcp') unless $port =~ /^\d+$/;
print "Listening on port $port...\n";
select(NS); $| = 1; select(STDOUT);
socket(S, AF_INET, SOCK_STREAM, $proto) || die "socket: $!";
$sockaddr = 'S n a4 x8';
$this = pack($sockaddr, AF_INET, $port, "\0\0\0\0");
bind(S, $this) || die "bind: $!";
listen(S, $channels) || die "listen: $!";
select(S); $| = 1; select(STDOUT);
# the meat of it:
for ($connection = 0; ; $connection++)
{
print "Waiting for connection $connection...\n";
($addr = accept(NS, S)) || die "accept: $!";
if (($child = fork()) == 0)
{
($af, $port, $inetaddr) = unpack($sockaddr, $addr);
@inetaddr = unpack('C4', $inetaddr);
print "Serving connection $connection at @inetaddr\n";
while (<NS>)
{
print "Received from client $connection: $_\n";
# this is where I want to echo to every client:
print NS "Server says: $_\n";
}
close(NS);
print "Client is no more. Forked server $connection exiting.\n";
exit;
}
close(NS);
}
--------------uncut
Any suggestions about how I should make this work? Thanks in advance.
--
Jason A. Gibb
webmaster@fargoweb.com
------------------------------
Date: Wed, 11 Jun 1997 12:06:27 +1000
From: Jon Hall <jon@atwww.com.au>
Subject: [Q]. Replacing a carriage return
Message-Id: <339E0823.CA6@atwww.com.au>
Hi,
This must be simple, but I just can't see it... I'm trying to replace
carriage returns (from a textarea on a WWW form) with <BR> tags. Here's
my best guess:-
$form_data{$message} =~ s/\\n/<BR>/g;
Any ideas?
Thanks,
Jon
------------------------------
Date: 10 Jun 1997 23:55:06 GMT
From: ez041407@bullwinkle.ucdavis.edu (Eric Finley)
Subject: C type macros?
Message-Id: <5nkpgq$c8n$1@mark.ucdavis.edu>
Is there a perl equivalent to a C macro. I find that I use some
expressions very frequently. My favorite seems to be /(\s+)?\n/ to
check for empty lines.
Any ideas?
Thanks,
Eric Finley
------------------------------
Date: 11 Jun 1997 00:32:11 GMT
From: nvp@shore.net (Nathan V. Patwardhan)
Subject: Re: crypt/decrypt in perl
Message-Id: <5nkrmb$711@fridge-nf0.shore.net>
Dennis Kowalski (Dennis.Kowalski@DaytonOH.NCR.com) wrote:
: I am currently using perl 5.001
I thought that you could use crypt() in NTPerl versions 5.001 PL 110 and
after? crypt() [like Unix crypt] is a one-way function, btw.
You can, however, compare an encrypted string against a clear text
string (and check the results), which is documented in _Programming
Perl_.
--
Nathan V. Patwardhan
nvp@shore.net
------------------------------
Date: 10 Jun 1997 17:44:21 -0600
From: mfuhr@dimensional.com (Michael Fuhr)
Subject: Re: Does print() buffer output?
Message-Id: <5nkosl$7s6@flatland.dimensional.com>
ajt@terlecki.demon.co.uk (Anthony J Terlecki) writes:
> When I run the following script I do not get the results I expected. What I
> wanted was the initial message following by a . every second. What I get is
> the initial message and all 9 dots at once 9 seconds after the script begins
> execution.
>
> Can anyone tell me why this is and how I might get around it?
Take a look at the Perl FAQ, Section 5:
http://www.perl.org/CPAN/doc/manual/html/pod/perlfaq5.html
> #!/usr/bin/perl
> print "processing";
> for ($i=1; $i<10; $i++) {
> print ".";
> sleep 1; # emulating a process in the loop taking time
> }
--
Michael Fuhr
http://www.dimensional.com/~mfuhr/
------------------------------
Date: Tue, 10 Jun 1997 16:52:24 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: City of Beverly Hills <ir003251@mindspring.com>
Subject: Re: Double Quote in replacement string
Message-Id: <Pine.GSO.3.96.970610164506.20918f-100000@kelly.teleport.com>
On Tue, 10 Jun 1997, City of Beverly Hills wrote:
> I use Perl script to insert href to specific strings in the html
> document.
Perl doesn't know (or care) that it's an HTML document. Consequently,
neither do I. :-) As far as we're concerned, you're just manipulating a
string.
> I would like to search the string for the variable and provide href to
> it, e.g replace phrase "Click $here" with "Click <A
> HREF="$here">$here</A>"
Well, I hate "click here", but maybe you want something like this?
s#Click $here#Click <A HREF= "$here" >$here</A>#;
> The problem is I can't incert double quote into the replacement
> string. I can incert the single quote though:
>
> $here="THERE";
> s?$here?"<A HREF='/subdir/" . $here . " .HTM'>" . $here . "</A>"?e;
Now the right side of the s/// is an expression in which you use
double-quoted strings. You can't just put a double quote as-is into a
double-quoted string! You have to backwhack it.
"I've got a \"quote mark\" or two."
Or, you could use alternate quotes, like this.
qq{I've got a "quote mark" or two.}
Hope this helps!
-- Tom Phoenix http://www.teleport.com/~rootbeer/
rootbeer@teleport.com PGP Skribu al mi per Esperanto!
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 10 Jun 1997 22:41:59 GMT
From: fox@pt0204.pto.ford.com (Ken Fox)
Subject: Re: embeding perl in C++
Message-Id: <5nkl7n$72m1@eccws1.dearborn.ford.com>
In article <339CF6F4.C1C@mentorg.com>, Chris Hill <Chris_Hill@mentorg.com> writes:
> Ken Fox wrote:
> > I'm working on a C++ library that makes it easier to use Perl with C++.
> > The library includes some pretty nice "glue" classes that hide (most of)
> > the gory details of calling Perl subroutines and working with Perl
> > data structures.
>
> Have you done anything with the XS interface between Perl and C++?
I prototyped an XS base class that you can derive from to create new XS
commands. The function object approach doesn't seem to work as well as the
xsub compiler because the user must convert between the calling conventions
manually. Xsubpp is really nice because it will automatically generate
the right code to convert between Perl and C++ calling conventions.
Have you tried SWIG? It's another approach very similar to xsubpp that
works with multiple target scripting languages (e.g. Tcl, Perl, Python).
> I ask this because we have embeded Perl into some C++ code. We have an
> abstract interface at the front end, so that we don't expose too much
> perl to the rest of the application.
One of the primary reasons for doing the C++ wrapper around the Perl
structures -- to keep the Perl #defines apart from the C++ code.
> However exposing application code
> into the Perl world is a bit of a problem - We expose our abstract
> interfaces through the XS modules and have some fairly kludgey code
> to handle upcasting of derived objects to base classes - to get the
> correct pointer arithmetic to happen.
I don't understand why you need to do kludgy pointer arithmetic to get
upcasting to work. C++ will automatically convert from derived to base
class without any special casts at all. When publishing a C++ interface
to Perl, I generally use lots of virtual methods. It works pretty well.
Sometimes I get a "fat" interface, but that just means I haven't quite
figured out the right type signatures.
> I keep thinging to myself that
> there must be a smarter, cleaner and more efficient way to do this.
Than xsubpp? I'm not so sure. Certainly we could use some extra tools
in our toolbox, and maybe an xsubpp that uses the C++ api would be nice
(to avoid namespace collisions). Integrating native code into Perl is
already much nicer than Tcl or (most of) the languages with more
agressive garbage collection.
- Ken
--
Ken Fox (kfox@ford.com) | My opinions or statements do
| not represent those of, nor are
Ford Motor Company, Powertrain | endorsed by, Ford Motor Company.
Analytical Powertrain Methods Department |
Software Development Section | "Is this some sort of trick
| question or what?" -- Calvin
------------------------------
Date: Tue, 10 Jun 1997 19:00:24 -0700
From: "Andre G. Assaiante" <andreg@corp.sgi.com>
Subject: HTML organization chart generator
Message-Id: <339E06B8.41C6@corp.sgi.com>
Folks:
Has anyone seen examples of a script that will generate a set of HTML
tables (an org chart) from a flat text file? I'm not looking for
anything too elaborate.
Any help is appreciated.
+===========================+==========================================+
| mail from | "I used to be 'with it' until |
| andreg@corp.sgi.com | they changed what 'it' was." |
| | |
| Andre G. Assaiante | Abe Simpson |
+===========================+==========================================+
------------------------------
Date: 10 Jun 1997 17:24:28 -0600
From: mfuhr@dimensional.com (Michael Fuhr)
Subject: Re: htons()
Message-Id: <5nknnc$7dp@flatland.dimensional.com>
Daniel Fox <dfox@pobox.com> writes:
> Is there a way to do htons() in perl?
Take a look at "pack" and "unpack" in the perlfunc manual page.
> Please reply by email.
If you'd like replies via email, please indicate that you'll
post a summary so others can learn as well. Since you didn't,
I've posted the answer to the newsgroup.
--
Michael Fuhr
http://www.dimensional.com/~mfuhr/
------------------------------
Date: 10 Jun 1997 20:02:17 -0400
From: 13j5;l@jumbo.com
Subject: MS-DOS port of the Perl programming language
Message-Id: <5nkpu9$se@orion.jumbo.com>
Get NEW MS-DOS port of the Perl programming language Shareware As It's Written!
The latest and greatest MS-DOS port of the Perl programming language shareware and
freeware as soon as it's created! We download new programs DAILY
(10 a.m. EST) from more than 300 sites all over the world.
Check out the new programs everyday in the "TODAY'S FREE COMPUTER
PROGRAMS" section on the all new JUMBO! --- bigger and faster
than ever with over 200,000 files and programs --- more than 1.8 million
links --- including a HUGE multimedia section with sounds, graphics, videos,
streaming music, VRML, animations, clip art, icons,
schockwave, screen savers --everything! --- http://www.jumbo.com.
Get the latest at:
http://www.jumbo.com/pages/programming/dos/perl/
http://www.jumbo.com/pages/programming/mac/languages/
------------------------------
Date: Wed, 11 Jun 1997 00:59:54 GMT
From: trit@olympic.seas.ucla.edu (Tri Duy Tram)
Subject: multiple white spaces
Message-Id: <EBL6ru.9on@seas.ucla.edu>
I am wondering how do you change multiple white spaces to just one single
white space? Lets suppose I have a string
$line = "This is a line that has multiple white space";
to
$line = "This is a line that has multiple white spaces";
I tried:
$_ = $line;
s/^\s*(.*?)\s*$/$1/;
but that gives me an error
/^\s*(.?)\s*$/: nested *?+ in regexp
any help will be appreciated. Thank you.
------------------------------
Date: 11 Jun 1997 00:01:07 GMT
From: e105578@orca.cc.metu.edu.tr (hakan keskin)
Subject: Re: newbie question about the sort function and hashes
Message-Id: <5nkps3$dpb@mogan.cc.metu.edu.tr>
Tom Phoenix (rootbeer@teleport.com) wrote:
: On Mon, 2 Jun 1997, Shane 'Fishman' Sherman wrote:
: > Subject: newbie question about the sort function and hashes
: Contrary to popular belief, it's not important to announce in the subject
: line that yours is a "newbie question". Usually that's obvious from the
: question alone. :-)
: > What i am trying to do is sort a hash by the keys but then print the
: > whole hash sorted and not just the keys...
: @sorted_keys = sort keys %hash;
: for (@sorted_keys) { print "$_ => $hash{$_}\n" }
: > print"How many lakes: ";
: > $numlakes=<STDIN>;
: > for($i=0;$i<$numlakes;$i+=1){
: > print "Name of lake: ";
: > $name=<STDIN>;
: > chomp($name);
: This is a somewhat unPerlish way of doing this. A more Perlian way might
: be something like this.
: while (1) {
: print "Enter a lake name (<CR> on empty line to end input) : ";
: chomp($name=<STDIN>);
: last unless defined $name;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I do not know why this line does not work..
but this works:
last unless ($name);
: ...
: > @sort = sort %lakeinfo;
: Ooh, that's not what you want. You're mixing the keys with the values!
: Hope this helps!
: -- Tom Phoenix http://www.teleport.com/~rootbeer/
: rootbeer@teleport.com PGP Skribu al mi per Esperanto!
: Randal Schwartz Case: http://www.lightlink.com/fors/
--
HAKAN KESKIN
E105578
------------------------------
Date: Tue, 10 Jun 1997 19:38:56 -0600
From: jmohr@grizzly.ccsd.k12.wy.us
To: nzawawi@worldnet.att.net
Subject: Re: Parsing Comma Delemited Text DataBase
Message-Id: <865988882.9079@dejanews.com>
In article <339AC84F.3832@worldnet.att.net>,
nzawawi@worldnet.att.net wrote:
>
> I trying to figure out how to parse a Text DataBase that is Comma
> delimeted and has "" Double quotes for its strings fields. My problem
> is withen these string fields you could have a comma that should not be
> parsed.
> Meaning
> 1,"Hello, World",6,"hi"
> Should be parsed into 4 fields
> 1. 1
> 2. "Hello, World"
> 3. 6
> 4. "hi"
>
> Not 5 fields
> 1. 1
> 2. "hello
> 3. World"
> 4. 6
> 5. "hi"
>
> I can't figure out the regulare expression that could do that for me.
> I tried to tackle this problem with both AWK and PERL with no luck.
> I would appreciate any feed back about this.
>
> Thanks,
> Nasser
Nasser,
I suggest you pick up a copy of
"Mastering Regular Expressions"
by Jeffrey E.F. Friedl
O'Reilly $ Associates, Inc.
This book has been a life saver to me, and has taught me much about
the way Perl handles regular expressions. On page 290 is an example
of using perl5 to parse CSV files. I've adapted Friedl's parse routine
into the following program. Again, get the book, it's WELL worth the
price.
John Mohr
Communications Specialist
Campbell County School District
Gillette, Wyoming
jmohr@grizzly.ccsd.k12.wy.us
#!/usr/bin/perl
# import-csv.pl
# Adapted from the work of Jeffrey Friedl
#
$workfile = shift;
$count = 1;
open (WORKFILE, "$workfile") || die "Can't open $workfile\n";
while (<WORKFILE>){ # print; @fields = (); push (@fields, $+) while $_
=~ m{ "([^\"\\]*(?:\\.[^\"\\]*)*)",? #standard quoted string, with
possible comma | ([^,]+),? #anything else, with possible comma | ,
#lone comma }gx; $count = @fields; print "$count\n";
print @fields;
}
-------------------==== Posted via Deja News ====-----------------------
http://www.dejanews.com/ Search, Read, Post to Usenet
------------------------------
Date: Tue, 10 Jun 1997 21:17:34 -0400
From: Nasser Al-Zawawi <nzawawi@worldnet.att.net>
Subject: Re: Parsing Comma Delemited Text DataBase
Message-Id: <339DFCAE.2A07@worldnet.att.net>
jmohr@grizzly.ccsd.k12.wy.us wrote:
>
> In article <339AC84F.3832@worldnet.att.net>,
> nzawawi@worldnet.att.net wrote:
> >
> > I trying to figure out how to parse a Text DataBase that is Comma
> > delimeted and has "" Double quotes for its strings fields. My problem
> > is withen these string fields you could have a comma that should not be
> > parsed.
> > Meaning
> > 1,"Hello, World",6,"hi"
> > Should be parsed into 4 fields
> > 1. 1
> > 2. "Hello, World"
> > 3. 6
> > 4. "hi"
> >
> > Not 5 fields
> > 1. 1
> > 2. "hello
> > 3. World"
> > 4. 6
> > 5. "hi"
> >
> > I can't figure out the regulare expression that could do that for me.
> > I tried to tackle this problem with both AWK and PERL with no luck.
> > I would appreciate any feed back about this.
> >
> > Thanks,
> > Nasser
>
> Nasser,
>
> I suggest you pick up a copy of
> "Mastering Regular Expressions"
> by Jeffrey E.F. Friedl
> O'Reilly $ Associates, Inc.
>
> This book has been a life saver to me, and has taught me much about
> the way Perl handles regular expressions. On page 290 is an example
> of using perl5 to parse CSV files. I've adapted Friedl's parse routine
> into the following program. Again, get the book, it's WELL worth the
> price.
>
> John Mohr
> Communications Specialist
> Campbell County School District
> Gillette, Wyoming
> jmohr@grizzly.ccsd.k12.wy.us
>
> #!/usr/bin/perl
> # import-csv.pl
> # Adapted from the work of Jeffrey Friedl
> #
> $workfile = shift;
> $count = 1;
> open (WORKFILE, "$workfile") || die "Can't open $workfile\n";
>
> while (<WORKFILE>){ # print; @fields = (); push (@fields, $+) while $_
> =~ m{ "([^\"\\]*(?:\\.[^\"\\]*)*)",? #standard quoted string, with
> possible comma | ([^,]+),? #anything else, with possible comma | ,
> #lone comma }gx; $count = @fields; print "$count\n";
>
> print @fields;
> }
>
> -------------------==== Posted via Deja News ====-----------------------
> http://www.dejanews.com/ Search, Read, Post to Usenet
Thanks alot. I will try it.
------------------------------
Date: Tue, 10 Jun 1997 21:22:09 -0400
From: Nasser Al-Zawawi <nzawawi@worldnet.att.net>
Subject: Re: Parsing Comma Delemited Text DataBase
Message-Id: <339DFDC1.5E17@worldnet.att.net>
Nem W Schlecht wrote:
>
> [courtesy copy e-mailed to author(s)]
>
> In comp.lang.perl.misc, Nasser Al-Zawawi <nzawawi@worldnet.att.net> wrote:
> >I trying to figure out how to parse a Text DataBase that is Comma
> >delimeted and has "" Double quotes for its strings fields. My problem
> >is withen these string fields you could have a comma that should not be
> >parsed.
> >Meaning
> >1,"Hello, World",6,"hi"
> >Should be parsed into 4 fields
> >1. 1
> >2. "Hello, World"
> >3. 6
> >4. "hi"
>
> Hmm.. since I don't see any real responces to your post... I wouldn't use a
> regex for this, since perl actually already knows exactly how to parse the
> above, let's just let perl do it with an eval():
>
> #!/local/bin/perl
> # Author: Nem W Schlecht
>
> while (<>) { # read in the text db
> chomp; # no newlines, please
> $_ = "\@input=($_)"; # add perl code to line
> eval($_); # presto!
> for (0..$#input) { # print it out
> print "$input[$_]\n";
> }
> }
>
> __END__
>
> If you don't what @input clobbered after each iteration, you'll have to
> copy it off or something.
>
> --
> Nem W Schlecht nem@plains.nodak.edu
> NDUS UNIX SysAdmin http://www.nodak.edu/~nem
> "Perl did the magic. I just waved the wand."
Thanks for your help. I will try this way too. It is great how there
is always many ways to do the same thing. It keeps creativity high.
Thanks to everyone who pitched in and helped me with my problem
Nasser
------------------------------
Date: Tue, 10 Jun 1997 20:21:17 -0400
From: TechMaster Pinyan <jefpin@bergen.org>
Subject: Passwording (on UNIX)
Message-Id: <Pine.SGI.3.96.970610202022.2026B-100000@davinci.bergen.org>
Is there a way to NOT show what characters a user is typing for the
purpose of entering passwords? Thanks in advance, even if no one knows.
----------------
| "Here we are now, entertain us!"
| - Nirvana
----------------
Jeff "TechMaster" Pinyan | http://www.bergen.org/~jefpin
HTML/CGI Designer and Consultant and JavaScripter
jefpin@bergen.org | TechMasterJeff@juno.com | TechMasterJeff@usa.net
Got a JavaScript question or problem? Let me know!
------------------------------
Date: Tue, 10 Jun 1997 20:13:47 -0600
From: psyclone@twd.net
Subject: Perl Compiler & Modules
Message-Id: <865991361.11269@dejanews.com>
Hi,
I've checked all the FAQ's and such, and I can't seem to find out how to
include "non-standard" modules in the perl compiler. I've got a few
scripts written using modules from CPAN, they seem to compile fine,
but when executed, they respond with
"Can't call method xxx in module xxx".
I know the compiler is still alpha, maybe I'll just have to wait for the
full release, or perl 5.005 :)
Anyone have any ideas, suggestions?
Thanks,
Allen
P.S. please respond via e-mail, as I don't have much usenet access.
-------------------==== Posted via Deja News ====-----------------------
http://www.dejanews.com/ Search, Read, Post to Usenet
------------------------------
Date: 11 Jun 97 02:06:27 GMT
From: rick@rick.infoserv.com (Rick Klement)
Subject: perl joke of the day ?
Message-Id: <87.7156@rick.infoserv.com>
Keywords: perl joke
I saw this line in a perl script today...
if ( length($aString) gt 0 )
--
Rick Klement - email: rick@infoserv.com
#include <standard.disclaimer>
RULE #5: When you can say something in a positive way or a negative way,
don't say it in the negative way.
------------------------------
Date: 11 Jun 1997 02:31:16 GMT
From: bod@compusol.com.au (Brendan O'Dea)
Subject: Re: Regexpert's assistance required.
Message-Id: <5nl2lk$4rp$1@diablo.compusol.com.au>
In article <5nih2s$muk$1@news.netusa.net>,
Eli the Bearded <usenet-tag@qz.little-neck.ny.us> wrote:
>Scott Blanksteen <sibsib@hotmail.com> wrote:
>> Simon Fairey wrote:
>[convert]
>> > item1, function1([0,11], function2() ), item2
>[to]
>> > item1
>> > function1([0,11], function2() )
>> > item2
>> If you know that your 'item1' and 'item2' won't have commas
>> in them, then just use
>
>Ah, come on. What if they do have commas? What then, huh, huh?
>
>This is a seriously difficult task for regular expressions. You
>are *much* better off using some other sort of parsing. The FAQ
>has got to have an entry on how to match properly nested parens
>to X deep. Go off and read that. Then maybe get a book on automata
>and learn why X must be fixed when matching with regexps. The
>reasons hold even for perl's superset of the math concept, I am
>quite confident. Certainly Friedl made no attempt to match parens
>more than two deep in his regexp to match email addresses for
>_Mastering Regular Expressions_.
The given example is actually fairly easy to parse with regular
expressions--it is just impossible with *one* RE.
While Jeff's e-mail example only handles nested parens to a fixed
depth, my understanding is that since he is providing this as a
complex example of how to construct a regular expression he has
restricted himself to a single RE for the task.
By example, the original posters field splitting exercise could be
handled with REs as follows:
#!/usr/bin/perl
@char = qw/, [ ] ( )/;
@cmap{@char} = map { chr } (0 .. $#char);
while (<>)
{
chomp;
while (/(\()([^][(),]*)(,?)([^][()]*)(\))/
or /(\[)([^][(),]*)(,?)([^][()]*)(\])/)
{
if ($3) { s//$1$2$cmap{$3}$4$5/ }
else { s//$cmap{$1}$2$4$cmap{$5}/ }
}
@field = map { s/([\x0-\x4])/$char[ord $1]/g; $_ }
split /\s*,\s*/;
for ($i = 0; $i < @field; $i++)
{
print "field[$i] => \"$field[$i]\"\n";
}
}
The while loop replaces first any commas within paired delimiters,
then the delimiters from the inside out by non-printing characters.
The fields are then split on any remaining commas, and the replaced
characters in each field are reverted to their original values.
Caveats:
* I haven't tested this much :-)
* It is assumed that the input data does not contain the characters
`nul' through `eot'.
* Quoting of characters via `\' or some other mechanism is not
handled.
The last two may be handled with an additional pre- and post-process,
omitted here for clarity (and laziness :-).
Regards,
--
Brendan O'Dea bod@compusol.com.au
Compusol Pty. Limited (NSW, Australia) +61 2 9809 0133
------------------------------
Date: Wed, 11 Jun 1997 00:15:20 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: Simple, but yet hard reg exp problem?
Message-Id: <EBL4pK.7zD@nonexistent.com>
Andreas Olsson (cie95aol@lustudat.student.lu.se) wrote on 1379 September
1993 in <URL: news:5nkbvn$3ko$1@news.lth.se>:
++
++ I have a text that looks like this:
++
++ <eq-1>a^{b}c</eq-1>
++ <eq-2>a^{b}c</eq-2>
++
++ Now I want to translate the ^{b} command different depending on if I'm
++ within
++ the <eq-1>...</eq-1> or the <eq-2>...</eq-2> tag. The final result should
++ look
++ like this:
++
++ <eq-1>a<super>b</super>c</eq-1>
++ <eq-2>a<inf>b</inf>c</eq-2>
++
++ I've tried the following but it doesn't work properly:
++
++ s#^{(.*?)}(.*?</eq-1)#<super>$1</super>$2#gs;
++ s#^{(.*?)}(.*?</eq-2)#<inf>$1</inf>$2#gs;
++
++ The problem appears when I have this combination of input
++
++ <eq-1>a^{b}c</eq-1>
++ <eq-2>a^{b}c</eq-2>
++ <eq-1>a^{b}c</eq-1>
++
++ When I find the ^ in the <eq-2> I don't want the first expression to do
++ anything, but it find </eq-1> on the other row and it substitutes incorrect.
++ I feel that this should be simple but can't find a way to solve it, please
++ help me.
Yes, you have tricked yourself by using .*? ".*?" doesn't mean match
the smallest substring such that the next character can match, it
means the smallest substring such that the *entire* regex matches.
(Well, that's not quite correct, but let's not dive in too many
details).
Also note that you cannot use regex to have it work for arbitrairy
nesting; Perls regexs are not powerful enough to match balanced
parens as a single regex. So we need to set a limit of nesting; lets
make it easy on ourselfs and don't allow nesting at all. Then
the following appears to work:
s#\^{([^}]*)}([^<]*</eq-1)#<super>$1</super>$2#gs;
s#\^{([^}]*)}([^<]*</eq-2)#<inf>$1</inf>$2#gs;
Note the escaped ^. Instead of using .*?, we do [^.]*, with . the
next char to be matched.
It'll get awful if you want to allow nesting. In that case, you
are much better of by writing a parser/tokenizer that builds
a parse tree, and given the parse tree, a subroutine writing
the new expression.
Abigail
--
perl5.004 -wMMath::BigInt -e'$^V=new Math::BigInt+qq;$^F$^W783$[$%9889$^F47$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W98$^F76777$=56;;$^U=$]*(q.25..($^W=@^V))=>do{print+chr$^V%$^U;$^V/=$^U}while$^V!=$^W'
------------------------------
Date: 10 Jun 1997 23:38:00 GMT
From: e105578@orca.cc.metu.edu.tr (hakan keskin)
Subject: Re: tr problems with variables
Message-Id: <5nkogo$qvl@mogan.cc.metu.edu.tr>
Dani Macho Ortiz (dani@apb.es) wrote:
: Hello, Perl News group!
: We are working under Solaris 2.5. Operating System
: and programming some CGI for Web Pages with GNU Perl 5.3.
: We've got a problem if we want to make a translation
: of characters an if these ones are defined into variables:
: $char1 = "x";
: $char2 = "w";
: $cad = "HelloxPerlxNewsx!";
: $_ = $cad;
: $changes = tr /$char1/$char2/;
: print "CHANGES: $changes\n";
Can I ask a question? ..
Where have you used $cad or $_ ?
: The previous code would print "CHANGES: 0"!!
: Do you know why and how could we do what we intend to?
: Thanks a lot.
:
: --
: -------------------------------------
: (
: ( )
: ( )
: /\ ( ) *
: / \| | * * *
: / \_|__ *
: / | *
: | [ ] | *
: | == |_______________________
: |__| |____|
: wwww wwwwwwwwwwwwwwwwwwwwwwwwwwww
: wwwww wwwwwwwwwwwwwwwwwwwwwwwwwww
: wwwwww wwwwwwwwwwwwwwwwwwwwwwwwww
: wwwwwww wwwwwwwwwwwwwwwwwwwwwwwww
:
: Dani Macho Ortiz
: Autoridad Portuaria de Barcelona
: Email: <dani@apb.es>
: http://www.apb.es
: -----------------------------------
--
HAKAN KESKIN
E105578
------------------------------
Date: Wed, 11 Jun 1997 00:07:26 GMT
From: Spiro@uh.edu (Spiro Angeli)
Subject: WIN32 modules Wanted
Message-Id: <339debe4.4179780@news.uh.edu>
Hi,
I am looking for some win32 module for Perl 5 for NT that will
allows me to manage the Windows NT Server.
I have noticed that QUE book explains them and perhaps has them on the
CD-ROM. Is there any possibility to find them on the NET?
Thank you. :-)
Spiro
------------------------------
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 598
*************************************