[10683] in Perl-Users-Digest
Perl-Users Digest, Issue: 4275 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Nov 22 19:07:29 1998
Date: Sun, 22 Nov 98 16:00:19 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 22 Nov 1998 Volume: 8 Number: 4275
Today's topics:
Re: How to make a pattern non-greedy in Perl 4 (I R A Aggie)
Re: How to make a pattern non-greedy in Perl 4 (David Huggins)
Re: HTTP_REFERER and Browser History (Martien Verbruggen)
Re: HTTPS - I cannot find a module on CPAN for this. <jhoglund@mirage.skypoint.net>
Re: Is there a case function in PERL? <tchrist@mox.perl.com>
MacPerl and makefiles <dejahvu@erols.com>
Mastering Regular Expressions (was Re: Recommend Perl b <uri@sysarch.com>
Re: Mastering Regular Expressions (was Re: Recommend Pe (Ilya Zakharevich)
Multicast UDP socket <ahansen@leland.Stanford.EDU>
Re: Regex for Matching Text Outside HTML Tags (Martien Verbruggen)
Re: Regular expression problem... <uri@sysarch.com>
Re: Regular expression problem... (Ilya Zakharevich)
Re: Round with Perl (Martien Verbruggen)
Re: Rounding dollar values (Tad McClellan)
Re: shopping cart (Peter)
Re: Sorting a flat file <rick.delaney@shaw.wave.ca>
Re: Sorting a flat file (Andre L.)
Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 22 Nov 1998 15:37:40 -0500
From: fl_aggie@thepentagon.com (I R A Aggie)
Subject: Re: How to make a pattern non-greedy in Perl 4
Message-Id: <fl_aggie-2211981537400001@aggie.coaps.fsu.edu>
In article <739lmq$g0l$1@newnews.global.net.uk>, "Martin"
<minich@globalnet.co.uk> wrote:
+ How can regexps be made non-greedy in Perl 4?
Upgrade to perl5? my impression -- its been a very long time since I've
seen this discussed -- was that perl4 regexs where always greedy, thus
the introduction of the '*?' construct.
James
------------------------------
Date: 22 Nov 1998 17:14:27 -0500
From: bn711@freenet.carleton.ca (David Huggins)
Subject: Re: How to make a pattern non-greedy in Perl 4
Message-Id: <871zmvs7cs.fsf@freenet.carleton.ca>
"Martin" <minich@globalnet.co.uk> writes:
> I've just found out the the ? modifier won't work in Perl 4.
>
> I have the regexp:
>
> s/abc(.*?)abc/$FOO{$1}/sg;
>
> which works fine under Perl 5 but Perl 4 won't accept the ? modifier to make
> the match .* non-greedy. I normally use Perl 5 so have never come across
> this problem before. How can regexps be made non-greedy in Perl 4?
Short answer: you can't. Pester root@localhost until they agree to
install Perl 5. If you are root@localhost, then install Perl 5!
Long answer: (note that I've only tested these on Perl 5 and they may
break in other ways on Perl 4...)
If the delimiters (i.e. what "abc" appears to represent in your
regex)are single characters, then you can usually get the same effect
by using a negated character class instead of dot, but this assumes
you don't want to allow escapes. (If you do want escapes, you can
still do it, though, but it's a bit trickier)
If the delimiters are constant strings, like "abc", then you can (and
probably should, since it's likely to be faster) just use 'index'
instead, like this: (it's ugly, but at least C programmers will
understand it)
my $pos = -1;
my $delim = "abc";
my $len = length $delim;
while (($pos = index ($_, $delim, $pos)) > -1) {
my $nextpos = index ($_, $delim, $pos+$len);
if ($nextpos > -1) {
my $keylen = $nextpos - $pos + $len;
substr ($_, $pos, $keylen) =
$FOO{substr ($_, $pos, $keylen)};
} else {
$pos += $len;
}
}
If they are regexes, then you could do the following, but it's really
evil, and is guaranteed to slow down your program everywhere, due to
the "unsociable $& and friends" effect:
my $pos = -1;
my $rex = "a[blurg]+c";
while (/$rex/g) {
my $rest=$';
my $len=length $&;
if ($rest =~ /$rex/g) {
my $keylen = pos ($rest) + $len;
pos $_ -= $len;
substr ($_, pos, $keylen) =
$FOO{substr ($_, pos, $keylen)};
}
}
Unfortunately, there's no way to avoid $& here, since Perl 4 doesn't
support zero-width lookahead either. Darn.
--
use strict;print&{sub{my(@q)=@_;&{sub{my($p)=@_;&$p($p,@q);}}(sub{my($p)=shift;
@_&&($_[0][0].":".(ref $_[0][1]?"\n".&$p($p,@{$_[0][1]}):$_[0][1])."\n".(&$p(
$p,@_[1..$#_])||''));});}}([name=>'David Huggins-Daines'],[email=>'bn711@free'.
'net.carleton.ca'],[occupation=>[[by_day=>'linguist'],[by_night=>'hacker']]]);
------------------------------
Date: Sun, 22 Nov 1998 22:56:59 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: HTTP_REFERER and Browser History
Message-Id: <%E062.33$wg2.108@nsw.nnrp.telstra.net>
In article <365751B6.23AF1FD3@clockwork.net>,
Dan Brian <dan@clockwork.net> writes:
> HTTP_REFERER (and all CGI ENV variables) are web-server-dependent
> variables; I don't know if HTTP_REFERER exists outside of Apache.
Nonsense. HTTP_REFERER is specified by the RFC and the original NCSA
documentation on CGI.
Of course, it is not specified by name, but under the general header
of 'The header lines received from the client' in the original NCSA
documentation, and under the HTTP_* header in the RFC.
You didn't only post irrelevant and off-topic material to clp.misc,
but it is also incorrect.
Martien
--
Martien Verbruggen |
Webmaster www.tradingpost.com.au | The gene pool could use a little
Commercial Dynamics Pty. Ltd. | chlorine.
NSW, Australia |
------------------------------
Date: 22 Nov 1998 20:05:20 GMT
From: Jamie Hoglund <jhoglund@mirage.skypoint.net>
Subject: Re: HTTPS - I cannot find a module on CPAN for this.
Message-Id: <739qq0$jks$1@shadow.skypoint.net>
dturley@pobox.com wrote:
: In article <36547936.BB62D616@keane.com>,
: Joseph DuBois <jdubois@keane.com> wrote:
:> Well met,
:>
:> I am looking for a module to work with a HTTPS server. I looked
:> on CPAN, but could not find either by catagory or module. Am I looking
:> for the wrong thing, or is there another way to do this.
: If I understand your question, you are looking at this incorrectly. HTTPS is
: basically a protocol for encrypting transmissions between a client browser
: and the web server. Calling a URL that starts with https simply uses secure
: socket layer (SSL) to encrypt the transmissions.
I suspect thats the problem.
It's true that a web server will decrypt it for you, I believe he needs
a way of POSTing data to a secure server, kind of like a browser might do.
In those cases, the script needs SSL support.
There is a tool called SSLeay but I haven't used it (yet). Unfortunately,
it requires other libraries and such, so it doesn't look like a very easy
thing to install.
Jamie
--
-------------------------------------------------------------------------
jamie@lecart.com
http://www.lecart.com
------------------------------
Date: 22 Nov 1998 23:26:27 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Is there a case function in PERL?
Message-Id: <73a6j3$6jk$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, "Timothy C. Pelham" <squid666@geocities.com> writes:
:Is there a function in PERL that does the same thing as the CASE
:function in C and if so what is it? If anyone knows please let me know
:at squid666@geocities.com. thanks
C has no `CASE function'.
Here's a document that might explain what Perl's switch statement
is. The short story is that it has one, and it's spelled "for()".
--tom
From: Tom Christiansen <tchrist@mox.perl.com>
Newsgroups: comp.lang.perl.misc
Subject: FMTEYEWTK on Switch Statements in Perl
In comp.lang.perl.misc, spiegler@cs.uri.edu writes:
:I cannot find a switch statement in Perl. Is there one?
man perlsyn or perlfaq7, or look at
http://www.perl.com/CPAN/doc/manual/html/pod/perlsyn.html#Basic_BLOCKs_and_Switch_Statemen
http://www.perl.com/CPAN/doc/manual/html/pod/perlfaq7.html#How_do_I_create_a_switch_or_case
:If not, do you know
:why it was not put into the language?
Because it seems unnecessary as is, and because no decision on something
more powerful has even been decided with.
:Also, does Perl provide any mechanism other than if-elsif-else to deal with
:this?
Many, but all can be placed into one of two classes, either multiple
sequential tests (`linear switches') or else a single that does a multiway
branch (`non-linear switches'). The first sort are all variations on
the same theme. You can affect performance minusculely by putting the
more likely cases first. You make a slight semantic distinction between
simple if()s and if/elsif()s chains. And you can make appearance choices
related to how to the tests are represented syntactically.
The short answer is that the switch statement in Perl is spelled for().
Here's a simple demo:
CASE 1:
for ($where) {
/In Card Names/ && do { push @flags, '-e'; last; };
/Anywhere/ && do { push @flags, '-h'; last; };
/In Rulings/ && do { last; };
die "unknown value for form variable where: `$where'";
}
But there is no performance gain to be had over here over a more
traditional arrangement. If you don't like that style, you can use this:
CASE 2:
SWITCH: for ($where) {
if (/In Card Names/) {
push @flags, '-e';
last SWITCH;
}
if (/Anywhere/) {
push @flags, '-h';
last SWITCH;
}
if (/In Rulings/) {
last SWITCH;
}
die "unknown value for form variable where: `$where'";
}
or you can use this:
CASE 3:
SWITCH: for ($where) {
if (/In Card Names/) {
push @flags, '-e';
}
elsif (/Anywhere/) {
push @flags, '-h';
}
elsif (/In Rulings/) {
; # nothing needed
}
else {
die "unknown value for form variable where: `$where'";
}
}
Or more concisely as this:
CASE 4:
SWITCH: for ($where) {
if (/In Card Names/) { push @flags, '-e'; }
elsif (/Anywhere/) { push @flags, '-h'; }
elsif (/In Rulings/) { } # do nothing
else { die "unknown whence: `$where'" }
}
There's really nothing to terrible with this, and no completely
convincing reason not to just write the elsif()s:
CASE 5:
chomp($answer = <>);
if ("SEND" =~ /^\Q$answer/i) { print "Action is send\n" }
elsif ("STOP" =~ /^\Q$answer/i) { print "Action is stop\n" }
elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" }
elsif ("LIST" =~ /^\Q$answer/i) { print "Action is list\n" }
elsif ("EDIT" =~ /^\Q$answer/i) { print "Action is edit\n" }
Or as:
CASE 6:
if ($visibility == SCREEN_DISPLAY) {
# do something
}
elsif ($visibility == SCREEN_HIDDEN) {
# do something
}
elsif ($visibility == SCREEN_CODEGEN) {
# do something
}
else {
# do something drastic
}
Many similar, sequential-testing mechanisms are seen in Perl.
You can use "&& ||" combos, but then you have to be very
careful that the RHS of && is always a true value.
CASE 7:
$dir = 'http://www.wins.uva.nl/~mes/jargon';
for ($ENV{HTTP_USER_AGENT}) {
$page = /Mac/ && 'm/Macintrash.html'
|| /Win(dows )?NT/ && 'e/evilandrude.html'
|| /Win|MSIE|WebTV/ && 'm/MicroslothWindows.html'
|| /Linux/ && 'l/Linux.html'
|| /HP-UX/ && 'h/HP-SUX.html'
|| /SunOS/ && 's/ScumOS.html'
|| 'a/AppendixB.html';
}
This is actually better written with a bunch of ?: constructs instead
in most cases because the RHS of the && might false out on you
in the more general case:
CASE 8:
$which_search = $opts{"t"} ? 'title'
: $opts{"s"} ? 'subject'
: $opts{"a"} ? 'author'
: 'title';
Or if you are assigning a list, you could do this:
CASE 9:
($msg, $defstyle) = do {
$i == 1 ? ("First", "Color" ) :
$i == 2 ? ("Then", "Rarity") :
("Then", "Name" )
};
Or even this, making more use of the do{}'s blockness:
CASE 10:
($FORMAT_NAME, $FORMAT_TOP_NAME) = do {
if ($long_form) { qw( Long Long_Top ) }
elsif ($just_inode) { qw( Inodes Inodes_Top ) }
elsif ($sys5) { qw( SysV SysV_Top ) }
elsif ($bsd) { qw( BSD BSD_Top ) }
else { die " No format?" }
};
Sometimes one has been known to use the do { } superfluously just to
make things look pretty. Sometimes, aesthetics counts: The do there is
just so the Christmas tree has a star. :-)
CASE 11:
for ($^O) {
*struct_flock = do {
/bsd/ && \&bsd_flock
||
/linux/ && \&linux_flock
||
/sunos/ && \&sunos_flock
||
die "unknown operating system $^O, bailing out";
};
}
Actually, this is an important point, easily overlooked. Aesthetics
really does count -- a lot. In virtually all these switch examples,
careful alignment of parallel constructs serves to clarify just
how the structure works. Good programmers put in extra effort
to make their code easy to read.
Here's another do{} style switch, turned another way:
CASE 12:
$amode = do {
if ($flag & O_RDONLY) { "r" } # XXX: isn't this 0?
elsif ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
elsif ($flag & O_RDWR) {
if ($flag & O_CREAT) { "w+" }
else { ($flag & O_APPEND) ? "a+" : "r+" }
}
};
Here's an old example for argument processing:
CASE 13:
while ($ARGV[0] =~ /^-(.+)/ && (shift, ($_ = $1), 1)) {
next if /^$/;
s/a// && (++$autoload, redo);
s/p// && (++$postcompile, redo);
s/f// && do {
$FILE = $_ || shift;
open(FILE) || die "can't open $FILE: $!";
while (<FILE>) {
push(@required, split(' '));
}
redo;
};
die "usage $0 [-pa] [-f module_list] [module] ...\n";
}
And here's another, simpler one:
CASE 14:
while ($ARGV[0] =~ /^-/) {
$ARGV[0] =~ /^-a/ && ($all++,shift,next);
$ARGV[0] =~ /^-d/ && ($debug++,shift,next);
$ARGV[0] =~ /^-n/ && ($idle = 0,shift,next);
$ARGV[0] =~ /^-s/ && ($slumber++,shift,next);
$ARGV[0] =~ /^-i/ && (shift,$idle=$ARGV[0],shift,next);
last;
}
And here's one more. I used to do this a lot. :-)
CASE 15:
OPT: while ($ARGV[0] =~ /^-(.*)/ && (shift, $_ = $1, 1)) {
/^$/ && next OPT;
/^\d+/ && do {
$DEPTH = $_;
next OPT;
};
s/q// && do {
$QUIET = 1;
redo OPT;
};
/d/ && do {
$debug += s/d//g;
redo OPT;
};
&usage("Unknown option: -$_");
}
As you see in the last case, the test doesn't always have to be the
same kind. There are definite advantages to this approach. For example:
CASE 16:
for ( $big->long->hairy->expression ) {
/pat/i && do { ...... };
$_ > 10 && do { ...... };
$_ == 13 && do { ...... };
uc eq "FRED" && do { ...... };
$now > 2*$_ && do { ...... };
# do default
}
In that case, we aren't in each case making the same sort of test (string,
numeric, pattern, etc). Instead, we pick and choose as we go.
But all the preceding versions share one critical trait: they all
perform a series of tests sequentially. That's why we called them
`linear switches' above. That means that they don't scale well to a
large number of cases. Of course, this really never matters all that
much, since in the number of different cases we're talking about here
is virtually always too small to fret over.
But any good programmer, when confronted with extremely similar code,
has a nearly irrepressible urge to unify. As Kernighan has said, one
should capture regularity with data, irregularity with code. That
means that seeing repeated instances of a nearly identical thing,
like this:
if ($who eq "Fred") { ... }
if ($who eq "Barney") { ... }
if ($who eq "Wilma") { ... }
makes any programmer worth his salt say, ``That's too much similarity
in code. It should be in data!'' And indeed, it probably should be,
and like most interesting data structures in Perl, this should likely
be based on a hash. (C and assembly programmers might think of this
as a jump table.)
Here's what you could do instead:
CASE 17:
%action = (
"Fred" => \&greet,
"Barney" => \&shuffle,
"Wilma" => \&chat,
# add as you will
);
if ($verb = $action{$who}) {
&$verb();
} else {
die "unknown character: $who";
}
Those of you corrupted into the infinite hunt for a one-liner
to everything might prefer this approach:
CASE 18:
{
"Fred" => \&greet,
"Barney" => \&shuffle,
"Wilma" => \&chat,
}->{$who}->();
Except that now you haven't managed to catch the null case.
CASE 19:
( {
"Fred" => \&greet,
"Barney" => \&shuffle,
"Wilma" => \&chat,
}->{$who} || sub { die: "who's $who?" })->();
But that's getting a bit silly. Better there to
do it in several steps.
These functions can even be defined right there as closures:
CASE 20:
%actions = (
"edit" => \&invoke_editor,
"send" => \&deliver_message,
"list" => sub { system($PAGER, $file) },
"abort" => sub {
print "See ya!\n";
exit;
},
"" => sub {
print "Unknown command: $cmd\n";
$errors++;
},
);
$href = abbrev(keys %actions);
local $_;
for (print "Action: "; <>; print "Action: ") {
s/^\s+//; # trim leading white space
s/\s+$//; # trim trailing white space
next unless $_;
$actions->{ $href->{ lc($_) } }->();
}
Imagine where you had a switch with 100 cases in a tight loop. You'd
definitely want to hash up the cases for instant access rather than
making all the tests each time. In fact, this would scale to any number
of cases (assuming that the hash keys weren't permutations of each other,
which pessimizes perl's hashing).
Here's an example of a non-linear switch with an in-line hash:
CASE 21:
my $newop = {
'==' => 'eq',
'!=' => 'ne',
'>=' => 'ge',
'>' => 'gt',
'<=' => 'le',
'<' => 'lt',
'=~' => '=~',
'!~' => '!~',
}->{$operator} || "MISSING OPERATOR";
It would be marginally better to assign that to a named hash once, as we
did in the previous case. Those of you who have coded up finite state
machines may be familiar with one final approach:
CASE 22:
while ($state = $action_table{$state}->()) {
print "Moving on to state $state\n";
}
Here, each function state in the action table returns the next state to
transition to. The table would hold as values the function to call at
each state, and those functions would return a string which is the next
state to go to.
So, anybody read this far? :-)
--tom
--
"A ship then new they built for him/of mithril and of elven glass"
--Larry Wall in perl.c from the v5.0 perl distribution,
citing Bilbo from Tolkien's LOTR
------------------------------
Date: Sun, 22 Nov 1998 16:23:51 -0600
From: Mary E Tyler <dejahvu@erols.com>
Subject: MacPerl and makefiles
Message-Id: <36588EF2.268B@erols.com>
Ak...
I tremble to ask but here I go again!
I am writing Perl code on my Mac and do most of my preliminary testing
there. I downloaded a module from CPAN and it has this makefile.pl which
crashes MacPerl. Someone tells me that MacPerl doesn't have that
capability... S'okay.... so how do I use the wonderful module?
Now let's make it more difficult. My target system is Unix... it's a CGI
script for my web site. What do I do with that module to make it work in
both places. If it makes any difference, I am with a web host and have
no control of the server environment etc.
Thank you in advance for any help.
Dejah
--
i trust i make myself obscure, i have need of obscurity now- robert bolt
a heated exchange of unread mail would be welcomed by all- christensen
Skating Fiction. Featuring the highly acclaimed serial On The Edge!
http://www.DejahsPrivateIce.com
------------------------------
Date: 22 Nov 1998 17:57:30 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Mastering Regular Expressions (was Re: Recommend Perl books?)
Message-Id: <x7pvaf1gkl.fsf_-_@sysarch.com>
>>>>> "DG" == Daniel Grisinger <dgris@moiraine.dimensional.com> writes:
DG> Uri Guttman <uri@sysarch.com> writes:
DG> Oh, yes. I wholeheartedly recommend it, but aspiring perl hackers
DG> should be warned that it is no longer completely accurate in its
DG> treatment of perl. It, like the camel, is a good book that is
DG> rapidly losing relevance as documentation of how perl behaves.
DG> New perl users especially need to be wary of this as they are the
DG> most likely to be bit by the differences.
this is true for any printed book. but most of the new regex features
being added are for expert or experienced perl hackers. not that newbies
couldn't use them but if you don't grok basic or intermediate regexes,
you won't even miss the new features. so MRE is more useful for them
even if it is slightly dated since it covers the core regex stuff so
well. the perl chapter is 100 pages and none of it is wrong except for
refering to some features (e.g. lookbehind) that now exist.
so it will always be very useful as a great education for regexes but not
as a reference to what actually can be done now. for that (as with the
camel) the online docs must be used. but even they are buggy in places. :-)
uri
--
Uri Guttman ----------------- SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire ---------------------- Perl, Internet, UNIX Consulting
uri@sysarch.com ------------------------------------ http://www.sysarch.com
The Best Search Engine on the Net ------------- http://www.northernlight.com
------------------------------
Date: 22 Nov 1998 23:34:11 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: Mastering Regular Expressions (was Re: Recommend Perl books?)
Message-Id: <73a71j$i7f$1@mathserv.mps.ohio-state.edu>
[A complimentary Cc of this posting was sent to Uri Guttman
<uri@sysarch.com>],
who wrote in article <x7pvaf1gkl.fsf_-_@sysarch.com>:
> this is true for any printed book. but most of the new regex features
> being added are for expert or experienced perl hackers. not that newbies
> couldn't use them but if you don't grok basic or intermediate regexes,
> you won't even miss the new features. so MRE is more useful for them
> even if it is slightly dated since it covers the core regex stuff so
> well. the perl chapter is 100 pages and none of it is wrong except for
> refering to some features (e.g. lookbehind) that now exist.
I think most things *are* wrong now. (Except the simplest ones, which
one should not bother to look into any book for.)
The book is still great as an educational tool, but any perl-specific
statement should be carefully checked against the documentation.
Especially those things which address problems of Perl's RExen, but
many other too. All the benchmarks are meaningless now.
This is my impression only, I did not try to comb through this chapter
with a highlight-pen and mark things are still-valid/invalid.
Ilya
------------------------------
Date: Sun, 22 Nov 1998 13:56:13 -0800
From: Andrew Hansen <ahansen@leland.Stanford.EDU>
Subject: Multicast UDP socket
Message-Id: <3658887D.2B73@leland.Stanford.EDU>
Hi folks,
I'm trying to get one of my existing perl programs
which logs UDP packets on a ``well known'' port to add
the current host to a multicast group. In c this can
be accomplished via call to
setsockopt(*sockfd_p, IPPROTO_IP, IP_ADD_MEMBERSHIP,
&(struct ip_mreq), sizeof(struct ip_mreq));
where the ip_mreq is filled with the appropriate info.
As far as I can tell Perl's Socket setsockopt doesn't
support said functionality. Am I missing something or
is there another way?
Thanks, Andrew
--
Andrew Hansen URL: http://www.stanford.edu/~ahansen/
496 Lomita Mall, Rm 250 email: ahansen@stanford.edu
Stanford University phone: 650.723.6754
Stanford, CA 94305-4035 fax: 650.725.5517
------------------------------
Date: Sun, 22 Nov 1998 22:50:06 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Regex for Matching Text Outside HTML Tags
Message-Id: <yy062.27$wg2.108@nsw.nnrp.telstra.net>
In article <365743BA.FCBAE1B9@clockwork.net>,
Dan Brian <dan@clockwork.net> writes:
> A quickie would be:
>
> @textonly = split(/<[^>]*>)/);
This will not compile.
> $textonly = join ('', split (/<[^>]*>/));
>
> or, to take all HTML tags out of a document,
>
> $textonly = $htmltext =~ s/<[^>]*>//g;
Have you seen what the perl FAQ has to say about this sort of
approach?
# perldoc perlfaq9
How do I remove HTML from a string?
[snip]
Many folks attempt a simple-minded regular expression
approach, like s/<.*?>//g, but that fails in many cases
because the tags may continue over line breaks, they may
contain quoted angle-brackets, or HTML comment may be
present.
[snip]
Martien
--
Martien Verbruggen |
Webmaster www.tradingpost.com.au | Little girls, like butterflies, need no
Commercial Dynamics Pty. Ltd. | excuse - Lazarus Long
NSW, Australia |
------------------------------
Date: 22 Nov 1998 18:06:43 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Regular expression problem...
Message-Id: <x7n25j1g58.fsf@sysarch.com>
>>>>> "DG" == Daniel Grisinger <dgris@moiraine.dimensional.com> writes:
DG> lr@hpl.hp.com (Larry Rosler) writes:
DG> <snip m/$ARGV[0][^\/]/ doesn't work>
>> $ARGV[0] = [];
>>
>> Now $ARGV[0] *is* a reference to array. How should the parser divine
>> that?
DG> Presumably the same way that it figures this out-
DG> DB<1> $a = 'this is a test'
DG> DB<2> $foo{foo}{foo} = '(?:\w+\s+)'
DG> DB<3> print qq.yup. if $a =~ /$foo{foo}{foo}{2}/
DG> yup
DG> DB<4>
this looks confusing. i tried a variant on yours:
DB<1> $a = 'this is a test'
DB<2> $foo[0][0] = '(?:\w+\s+)'
DB<3> print "foo" if $a =~ /$foo[0][0]{2}/
foo
DB<4> print "foo" if $a =~ /$foo[0][0][1-9]/
foo
DB<5> x $foo[0][0]
0 '(?:\\w+\\s+)'
why would the regex work. there are no digits in $a.
DB<6> print "foo" if $a =~ /$foo[0][0]\d/
DB<7>
that fails like it should. this implies that [1-9] was interpreted as 1
minus 9 and a subscript which didn't exist. so it was undef which used
the last successful regex.
so the regex parser is using some knowledge of the data as a heuristic
in parsing.
this area should be documented better. better yet, don't write regexes
that look like that! it is bad coding to use arrays and hashes where you
could find char classes and count modifiers.
uri
--
Uri Guttman ----------------- SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire ---------------------- Perl, Internet, UNIX Consulting
uri@sysarch.com ------------------------------------ http://www.sysarch.com
The Best Search Engine on the Net ------------- http://www.northernlight.com
------------------------------
Date: 22 Nov 1998 23:35:58 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: Regular expression problem...
Message-Id: <73a74u$i9o$1@mathserv.mps.ohio-state.edu>
[A complimentary Cc of this posting was sent to Uri Guttman
<uri@sysarch.com>],
who wrote in article <x7n25j1g58.fsf@sysarch.com>:
> so the regex parser is using some knowledge of the data as a heuristic
> in parsing.
>
> this area should be documented better. better yet, don't write regexes
> that look like that! it is bad coding to use arrays and hashes where you
> could find char classes and count modifiers.
I recall that one of my `use pedantic' or `use strict code' proposals
is to make such ambiguities a fatal error.
Ilya
------------------------------
Date: Sun, 22 Nov 1998 23:07:46 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Round with Perl
Message-Id: <6P062.44$wg2.108@nsw.nnrp.telstra.net>
[alt.perl? There's no such beast over here, removed]
In article <Un3NuHgI1diW-pn2-TfS5m6UvffbK@armi.dyn.icon.fi>,
tumppi@nospam.icon.fi (Tuomas Angervuori) writes:
> I have a problem: for example when I divide 1 with 3, I get result
> 0.3333333333....
You call that a problem? I call that wanted behaviour :)
> How can I tell Perl to count only for example two desimals?
# perldoc perlfaq4
Does perl have a round function? What about ceil() and
floor()? Trig functions?
# perldoc -f sprintf
# perldoc -f printf
Martien
--
Martien Verbruggen |
Webmaster www.tradingpost.com.au | Little girls, like butterflies, need no
Commercial Dynamics Pty. Ltd. | excuse - Lazarus Long
NSW, Australia |
------------------------------
Date: Sun, 22 Nov 1998 14:26:57 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Rounding dollar values
Message-Id: <h2s937.in7.ln@flash.net>
Scott Thompson (scotty@nospam.internetbrokers.ab.ca) wrote:
: What's the easiest way to round up results of a calculation, and limit
: it to 2 decimal places?
The easiest way is to look up the answer in the Perl FAQ.
Posting to Usenet is much harder...
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 22 Nov 1998 21:55:49 GMT
From: peter@helpnet.com.au (Peter)
Subject: Re: shopping cart
Message-Id: <365883cc.2764812@news.flex.com.au>
Hello Mike and others,
I am looking at the shopping cards offered free from the sites you
mentioned.
I did not set out to write a shopping cart. I tried to get 3
different ISPs to quote for a shopping cart. All 3 said I wanted
something new, difficult to do and vvvvvery expensive.
All 3 quoted a rate per hour for programming that is far higher than
anything that is being paid to programmers.
All 3 refused to give even an indication of price.
All 3 lost the business.
All 3 lost the ongoing business from several other companies following
my recommendations.
Given that every major book store has several C and Perl programming
books that show how to write a shopping cart and that most have the
code on CD, how can ISPs pretend a shopping cart has to be invented
from scratch at great expense?
Peter
On 19 Nov 1998 22:25:50 GMT, Mike West <westmj@esvax.dnet.dupont.com>
wrote:
>In article <36541CCD.AEF19CB0@catnmoose.com> Marty Landman,
> marty@catnmoose.com writes:
>>Dustin Puryear <dpuryear@usa.net> wrote:
>>
>>>I'm looking for a free shopping cart program that is well
>>>documented. Any suggestions?
>>
>>I'm in the same boat and have picked up the (free) distribution of
>>Commerce.cgi from http://www.dial411.com/
>
>>Curious to hear the opinion of others
>
>Why reinvent the wheel? Get http://www.minivend.com/ for free.
>In perl, with source. And be amazed.
------------------------------
Date: Sun, 22 Nov 1998 20:31:21 GMT
From: Rick Delaney <rick.delaney@shaw.wave.ca>
Subject: Re: Sorting a flat file
Message-Id: <36587649.96157C10@shaw.wave.ca>
[posted & mailed]
shienh gurpreet wrote:
>
> Hello!!
> I tried looking at the perl FAQ but couldn't figure out how they were
> sorting the array with respect to any field. Here's the example
> database file I have, and I wanna sort it w.r.t. the 5th column (with
> %) in ascending order. Can someone please help as you have done
> before. Thank you.
>
> eir 11 9 2 81% 13
> oos 13 6 4 60% 25
> hrh 12 6 8 50% 15
> spp 10 6 4 60% 14
This comes from http://www.perl.com/CPAN/doc/FMTEYEWTK/sort.html, right?
Read this and reread it, trying out each step-by-step example.
Enlightenment will follow.
It sounds like you're stuck on the part that references the field you
want.
Take a look at CODE 1.1:
sub by_last_num_field {
@alist = split(/\s+/, $a);
@blist = split(/\s+/, $b);
if ( $alist[$#alist] > $blist[$#blist] ) {
return 1;
} elsif ( $blist[$#blist] > $alist[$#alist] ) {
return -1
} else {
return 0;
}
}
In this document, the example is meant to sort by the last field.
$#alist refers to the last subscript of the array @alist, so
$alist[$#alist] is the last element of @alist (or the last field of your
record.)
In your case, you want the 5th field which would be the fifth element of
@alist which is $alist[4] ($alist[0] is the first element).
This should get you past CODE 1.1. Read on, there is much more to learn
from this document. Also check out the perldata page that comes with
your perl distribution for more information about arrays.
--
Rick Delaney
rick.delaney@shaw.wave.ca
------------------------------
Date: Sun, 22 Nov 1998 17:45:48 -0500
From: alecler@cam.org (Andre L.)
Subject: Re: Sorting a flat file
Message-Id: <alecler-2211981745480001@dialup-637.hip.cam.org>
In article <365868BD.73BEE0D9@roogna.eng.wayne.edu>, shienh gurpreet
<gss2@roogna.eng.wayne.edu> wrote:
> Hello!!
> I tried looking at the perl FAQ but couldn't figure out how they were
> sorting the array with respect to any field. Here's the example database
> file I have, and I wanna sort it w.r.t. the 5th column (with %) in
> ascending order. Can someone please help as you have done before. Thank
> you.
This is a job for the Schwartzian transform (see perlfaq 4). (Notice the
twist I use in the split, to separate the % from the number for the
purpose of numerical sorting.)
@data = map { $_->[0] }
sort { $a->[5] <=> $b->[5] }
map { [ $_, split /\s+|(?=%)/ ] } <DATA>;
print @data;
__DATA__
eir 11 9 2 81% 13
oos 13 6 4 60% 25
hrh 12 6 8 50% 15
spp 10 6 4 60% 14
The output:
hrh 12 6 8 50% 15
oos 13 6 4 60% 25
spp 10 6 4 60% 14
eir 11 9 2 81% 13
HTH,
Andre
------------------------------
Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>
Administrivia:
Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.
If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu.
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 4275
**************************************