[24912] in Perl-Users-Digest
Perl-Users Digest, Issue: 7162 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Sep 21 21:38:27 2004
Date: Tue, 21 Sep 2004 18:37:01 -0700 (PDT)
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, 21 Sep 2004 Volume: 10 Number: 7162
Today's topics:
regular expression could do this?(newbie) <end@dream.life>
Re: regular expression could do this?(newbie) <wfsp@removeyahoo.com>
Re: regular expression could do this?(newbie) <end@dream.life>
Re: regular expression could do this?(newbie) (Jim Keenan)
Replacing spaces (Aristotle)
Re: Replacing spaces <matrix_calling@yahoo.dot.com>
Re: Replacing spaces <jurgenex@hotmail.com>
Re: Replacing spaces <eberhard.niendorf@epost.de>
Re: Replacing spaces <jurgenex@hotmail.com>
Re: Replacing spaces <eberhard.niendorf@epost.de>
Re: Replacing spaces <matrix_calling@yahoo.dot.com>
Re: Replacing spaces (Anno Siegel)
Re: Replacing spaces (Anno Siegel)
Re: Rounding error in program ctcgag@hotmail.com
run a sub referenced in hash. <asf@sadfkj.cosdf>
Re: run a sub referenced in hash. <matrix_calling@yahoo.dot.com>
Re: run a sub referenced in hash. <noreply@gunnar.cc>
Re: run a sub referenced in hash. <spamtrap@dot-app.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 21 Sep 2004 11:06:51 +0800
From: Alont <end@dream.life>
Subject: regular expression could do this?(newbie)
Message-Id: <41519803.43923109@130.133.1.4>
I want to pattern a text block, but the text block very large(and
multi-line), the first line should be:
<html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
and the end of the text block:
rel="external" target="new">Forum</a></li>
</ul>
</div>
</div>
</div>
so, how I can pattern the text block in a html file(many html files
waiting for pattern and then replace to"<!-- #include
virtual="/Head.inc" -->")
I have seen much examples, but can't find a example could do this
--
Your fault as a Government is My failure as a citizen
------------------------------
Date: Tue, 21 Sep 2004 07:35:32 +0000 (UTC)
From: "wfsp" <wfsp@removeyahoo.com>
Subject: Re: regular expression could do this?(newbie)
Message-Id: <ciolk4$od5$1@sparta.btinternet.com>
"Alont" <end@dream.life> wrote in message
news:41519803.43923109@130.133.1.4...
>I want to pattern a text block, but the text block very large(and
> multi-line), the first line should be:
> <html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
>
> and the end of the text block:
> rel="external" target="new">Forum</a></li>
> </ul>
> </div>
> </div>
> </div>
>
> so, how I can pattern the text block in a html file(many html files
> waiting for pattern and then replace to"<!-- #include
> virtual="/Head.inc" -->")
>
> I have seen much examples, but can't find a example could do this
> --
> Your fault as a Government is My failure as a citizen
Using regexs on HTML is _very_ difficult; especially "many" "very large"
files. My advice would be to not even consider it. There are many good
modules to parse HTML (I use HTML::Tokeparser) and I would urge you to have
a look at them. If you hit any snags come back with what you have tried and
we'll see how we go from there.
Best of luck.
------------------------------
Date: Tue, 21 Sep 2004 16:26:48 +0800
From: Alont <end@dream.life>
Subject: Re: regular expression could do this?(newbie)
Message-Id: <414fe578.63752093@130.133.1.4>
"wfsp" <wfsp@removeyahoo.com>Wrote at Tue, 21 Sep 2004 07:35:32 +0000
(UTC):
>Using regexs on HTML is _very_ difficult; especially "many" "very large"
>files. My advice would be to not even consider it. There are many good
>modules to parse HTML (I use HTML::Tokeparser) and I would urge you to have
>a look at them. If you hit any snags come back with what you have tried and
>we'll see how we go from there.
>Best of luck.
>
I'll try what you say, thank you:)
--
Your fault as a Government is My failure as a citizen
------------------------------
Date: 21 Sep 2004 06:56:30 -0700
From: jkeen_via_google@yahoo.com (Jim Keenan)
Subject: Re: regular expression could do this?(newbie)
Message-Id: <196cb7af.0409210556.217f6f18@posting.google.com>
Alont <end@dream.life> wrote in message news:<41519803.43923109@130.133.1.4>...
> I want to pattern a text block, but the text block very large(and
> multi-line), the first line should be:
> <html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
>
> and the end of the text block:
> rel="external" target="new">Forum</a></li>
> </ul>
> </div>
> </div>
> </div>
>
> so, how I can pattern the text block in a html file(many html files
> waiting for pattern and then replace to"<!-- #include
> virtual="/Head.inc" -->")
>
The keys to solving a regex like this are: (1) use the 's' qualifier
so '\n' gets counted in '.'; (2) use the 'x' qualifier so that you can
include comments and whitespace within the substitution code; (3)
build up the successful matches incrementally. I built up the
successful match using the commented-out lines below beginning with
'if'.
my $str = '<html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
text in the middle:
rel="external" target="new">Forum</a></li>
</ul>
</div>
</div>
</div>
';
print $str, "\n";
# if ($str =~ s{<html><!DOCTYPE\shtml\sPUBLIC\s"}
# if ($str =~ s{<html><!DOCTYPE\shtml\sPUBLIC\s"-\/\/W3C}
# if ($str =~ s{<html><!DOCTYPE\shtml\sPUBLIC\s"-\/\/W3C\/\/DTD\sXHTML\s1.0\s}
# if ($str =~ s{<html><!DOCTYPE\shtml\sPUBLIC\s"-\/\/W3C\/\/DTD\sXHTML\s1.0\sTransitional\/\/EN"\n}
# failure
if ($str =~ s{<html><!DOCTYPE\shtml\sPUBLIC\s"-\/\/W3C\/\/DTD\sXHTML\s1.0\sTransitional\/\/EN"\s
.*\s
rel="external"\starget="new">Forum<\/a><\/li>\s
\s+<\/ul>\s
\s+<\/div>\s
\s+<\/div>\s
<\/div>\s
} # end of pattern to be matched
{"<!-- #include virtual="\/Head.inc" -->"}sx # text to be
substituted
# qualifiers to make \n work as \s, ignore whitespace and
comments
) # end of 'if' condition
{
print "Success! String is now:\n";
print "$str\n";
} else {
print "Failure\n";
}
------------------------------
Date: 19 Sep 2004 02:39:20 -0700
From: qwerty97654@hotmail.com (Aristotle)
Subject: Replacing spaces
Message-Id: <9a1882b6.0409190139.51c55d6@posting.google.com>
I'm trying to replace spaces at the beggining of a string, with
" " .
Not all spaces by a single , but rather each space by a single
" "
eg
" make love not war" --> " make love not
war"
" follow the white rabbit" --> " follow the white rabbit"
ie " " should replace only the beggining spaces (one by one), but
not other spaces.
The way i'm doing this for now is
$string =~ s/ /\ \;/g;
$string =~ s/([A-Za-z])\ \;([A-Za-z])/$1 $2/g;
ie, first replacing all spaces with , then replacing again
those between two words. It gets the job somewhat done (a bit
inefficiently, since if there are other characters within the string
(like ",.-:;" ) the " " arent being replaced).
If i try to use $string =~ s/^\s+/\ \;/; then all beggining spaces
are being replaced by a single " ", while what i need is the
number of " " to match the number of spaces at the beggining of
the string.
I'd appreciate your help on this.
Thank you in advance.
------------------------------
Date: Sun, 19 Sep 2004 15:58:13 +0530
From: Abhinav <matrix_calling@yahoo.dot.com>
Subject: Re: Replacing spaces
Message-Id: <Mld3d.1$1M5.112@news.oracle.com>
Aristotle wrote:
> I'm trying to replace spaces at the beggining of a string, with
> " " .
> Not all spaces by a single , but rather each space by a single
> " "
>
> eg
> " make love not war" --> " make love not
> war"
> " follow the white rabbit" --> " follow the white rabbit"
>
> ie " " should replace only the beggining spaces (one by one), but
> not other spaces.
>
> The way i'm doing this for now is
>
> $string =~ s/ /\ \;/g;
> $string =~ s/([A-Za-z])\ \;([A-Za-z])/$1 $2/g;
>
I'm just learning these kind of things, but you can replace those two with
$string =~ s/^([ ](?{ $cnt .= "\ \;"}))*/$cnt/;
Worked with the spaces ..
Awaiting critiques from the regulars .. :)
Abhinav
------------------------------
Date: Sun, 19 Sep 2004 10:50:28 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Replacing spaces
Message-Id: <Uvd3d.3067$PZ4.2536@trnddc06>
Aristotle wrote:
> I'm trying to replace spaces at the beggining of a string, with
> " " .
> Not all spaces by a single , but rather each space by a single
> " "
>
> eg
> " make love not war" --> " make love not
> war"
> " follow the white rabbit" --> " follow the white rabbit"
>
> ie " " should replace only the beggining spaces (one by one), but
> not other spaces.
One way to do it:
s/^(\s*)/' ' x length $1/e;
jue
------------------------------
Date: Sun, 19 Sep 2004 13:22:25 +0200
From: Eberhard Niendorf <eberhard.niendorf@epost.de>
Subject: Re: Replacing spaces
Message-Id: <cijplg$mg9$06$1@news.t-online.com>
Aristotle <qwerty97654@hotmail.com> wrote:
> I'm trying to replace spaces at the beggining of a string, with
> " " .
> Not all spaces by a single , but rather each space by a single
> " "
>
> eg
> " make love not war" --> " make love not
> war"
> " follow the white rabbit" --> " follow the white rabbit"
>
> ie " " should replace only the beggining spaces (one by one), but
> not other spaces.
>
> The way i'm doing this for now is
>
> $string =~ s/ /\ \;/g;
> $string =~ s/([A-Za-z])\ \;([A-Za-z])/$1 $2/g;
>
This should work
1 while ( $string =~ s/^\s/\ /g );
Eberhard
------------------------------
Date: Sun, 19 Sep 2004 11:23:00 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Replacing spaces
Message-Id: <o_d3d.3070$PZ4.818@trnddc06>
Eberhard Niendorf wrote:
> Aristotle <qwerty97654@hotmail.com> wrote:
>
>> I'm trying to replace spaces at the beggining of a string, with
>> " " .
>> Not all spaces by a single , but rather each space by a single
>> " "
>>
>> eg
>> " make love not war" --> " make love not
>> war"
>> " follow the white rabbit" --> " follow the white rabbit"
>>
>> ie " " should replace only the beggining spaces (one by one),
>> but not other spaces.
> This should work
>
> 1 while ( $string =~ s/^\s/\ /g );
Why didn't you test it?
Your while loop succeeds exactly once, then there is no leading space any
longer and all remaining spaces will, well, remain.
jue
------------------------------
Date: Sun, 19 Sep 2004 13:35:00 +0200
From: Eberhard Niendorf <eberhard.niendorf@epost.de>
Subject: Re: Replacing spaces
Message-Id: <cijqd2$v8c$02$1@news.t-online.com>
Jürgen Exner <jurgenex@hotmail.com> wrote:
>> This should work
>>
>> 1 while ( $string =~ s/^\s/\ /g );
>
> Why didn't you test it?
> Your while loop succeeds exactly once, then there is no leading space any
> longer and all remaining spaces will, well, remain.
>
> jue
Sorry, shame on me! You are right I was WRONG, I've wrong tested.
Eberhard
------------------------------
Date: Sun, 19 Sep 2004 17:48:32 +0530
From: Abhinav <matrix_calling@yahoo.dot.com>
Subject: Re: Replacing spaces
Message-Id: <bZe3d.2$1M5.48@news.oracle.com>
Eberhard Niendorf wrote:
> Aristotle <qwerty97654@hotmail.com> wrote:
>
>
>>I'm trying to replace spaces at the beggining of a string, with
>>" " .
>>Not all spaces by a single , but rather each space by a single
>>" "
>>
>>eg
>>" make love not war" --> " make love not
>>war"
>>" follow the white rabbit" --> " follow the white rabbit"
>>
>>ie " " should replace only the beggining spaces (one by one), but
>>not other spaces.
>>
>>The way i'm doing this for now is
>>
>> $string =~ s/ /\ \;/g;
>> $string =~ s/([A-Za-z])\ \;([A-Za-z])/$1 $2/g;
>>
>
>
> This should work
>
> 1 while ( $string =~ s/^\s/\ /g );
>
I tried this before brewing that complex concoction up-thread .. It will
not work as the match succeeds only once ..
Of course, Jurgen's solution is better than mine :)
Regards
Abhinav
------------------------------
Date: 20 Sep 2004 08:40:52 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Replacing spaces
Message-Id: <cim52k$hth$1@mamenchi.zrz.TU-Berlin.DE>
Abhinav <matrix_calling@yahoo.dot.com> wrote in comp.lang.perl.misc:
> Aristotle wrote:
> > I'm trying to replace spaces at the beggining of a string, with
> > " " .
> > Not all spaces by a single , but rather each space by a single
> > " "
> >
> > eg
> > " make love not war" --> " make love not
> > war"
> > " follow the white rabbit" --> " follow the white rabbit"
> >
> > ie " " should replace only the beggining spaces (one by one), but
> > not other spaces.
> >
> > The way i'm doing this for now is
> >
> > $string =~ s/ /\ \;/g;
> > $string =~ s/([A-Za-z])\ \;([A-Za-z])/$1 $2/g;
> >
>
> I'm just learning these kind of things, but you can replace those two with
>
> $string =~ s/^([ ](?{ $cnt .= "\ \;"}))*/$cnt/;
>
> Worked with the spaces ..
>
> Awaiting critiques from the regulars .. :)
Well, it doesn't run under strictures. Also, $cnt should be cleared
before each call, otherwise " "s would collect in it.
Anno
------------------------------
Date: 20 Sep 2004 09:00:37 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Replacing spaces
Message-Id: <cim67l$jev$2@mamenchi.zrz.TU-Berlin.DE>
Jürgen Exner <jurgenex@hotmail.com> wrote in comp.lang.perl.misc:
> Aristotle wrote:
> > I'm trying to replace spaces at the beggining of a string, with
> > " " .
> > Not all spaces by a single , but rather each space by a single
> > " "
> >
> > eg
> > " make love not war" --> " make love not
> > war"
> > " follow the white rabbit" --> " follow the white rabbit"
> >
> > ie " " should replace only the beggining spaces (one by one), but
> > not other spaces.
>
> One way to do it:
>
> s/^(\s*)/' ' x length $1/e;
Here's another:
/ */g;
substr( $_, 0, pos) =~ s/ / /g;
Anno
------------------------------
Date: 19 Sep 2004 16:51:28 GMT
From: ctcgag@hotmail.com
Subject: Re: Rounding error in program
Message-Id: <20040919125128.378$Gn@newsreader.com>
Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote:
> On 17 Sep 2004, A. Sinan Unur wrote:
>
> >You snipped the relevant part of Helgi's post. Reading what you are
> >responding to helps:
> >
> >Helgi Briem <HelgiBriem_1@hotmail.com> wrote in
> >news:46olk096ruu12k2spgno4jvjpsgiusvvdo@4ax.com:
> >
> >>> Always rounding X.5 *up* as you probably learned in grade school
> >>> introduces a systematic bias. This was changed by the International
> >>> Standards Organisation in 1992, to rounding to the nearest even
> >>> integer.
>
> Does anyone know the ISO's reasoning behind it?
So that you round up just about as often as you round down, so they
cancel each other out--rather than rounding up more often than down.
> It seems to create a new
> bias,
Normally, no one cares what the even/odd distribution of the least
siginificant digit of a column of numbers are, so the bias it introduces is
harmless.
> which doesn't seem to make sense to me, at least.
None of it makes sense to me. If you are going to be summing a column
of numbers, don't round them first.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Sun, 19 Sep 2004 11:04:36 -0400
From: "nntp" <asf@sadfkj.cosdf>
Subject: run a sub referenced in hash.
Message-Id: <10kr81k1pinea50@corp.supernews.com>
I am trying to run a sub refferenced in a hash.. is there any way to do
this? this is what I tried.
#!/usr/bin/perl
#######################################################
%caps= ( red => 'subONE', blue => 'subTWO', green => 'GREEN' );
$tmp = "red"; #
$arg = "sting to be passed to sub..."; # assign some fake values for now.
&$caps{$tmp}->($arg); # attempt to call the sub....
sub subONE{
print "One recieved @_[0] \n";
}
sub subTWO{
print "Two recieved @_[0] \n";
}
###########################################################3
I just cant seem to get it going... I've tried several different things..
any ideas?
------------------------------
Date: Sun, 19 Sep 2004 20:58:23 +0530
From: Abhinav <matrix_calling@yahoo.dot.com>
Subject: Re: run a sub referenced in hash.
Message-Id: <8Lh3d.3$1M5.55@news.oracle.com>
nntp wrote:
> I am trying to run a sub refferenced in a hash.. is there any way to do
> this? this is what I tried.
>
> #!/usr/bin/perl
>
use strict;
use warnings;
>
> #######################################################
> %caps= ( red => 'subONE', blue => 'subTWO', green => 'GREEN' );
>
> $tmp = "red"; #
> $arg = "sting to be passed to sub..."; # assign some fake values for now.
>
> &$caps{$tmp}->($arg); # attempt to call the sub....
>
This should be
$caps{$tmp}($arg);
Caveat !!
use strict gives the error :
Can't use string ("subONE") as a subroutine ref while "strict refs" in use
at t1.pl line 10.
And AFAIK, it is considered BAD practice to be doing this. (Which is why
use strict does not allow it in the first place)
I guess this is the right place to know more about this :
perldoc perlref
>sub subONE{
> print "One recieved @_[0] \n";
^^^^
This should be $_[0]
>}
>sub subTWO{
> print "Two recieved @_[0] \n";
^^^^^
Same here ..
[SNIP] ..
Regards
Abhinav
------------------------------
Date: Sun, 19 Sep 2004 17:38:55 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: run a sub referenced in hash.
Message-Id: <2r5nfoF15qbpqU1@uni-berlin.de>
nntp wrote:
> I am trying to run a sub refferenced in a hash.. is there any way to do
> this? this is what I tried.
>
> #!/usr/bin/perl
Where are
use strict;
use warnings;
?? Perl would have helped you a great deal if strictures and warnings
had been enabled. You should not post here without first trying to
have Perl help you.
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
> %caps= ( red => 'subONE', blue => 'subTWO', green => 'GREEN' );
-------------------^^^^^^^^----------^^^^^^^^
They are not subreferences; they are plain strings.
my %caps = (red => \&subONE, blue => \&subTWO, green => 'GREEN');
> &$caps{$tmp}->($arg); # attempt to call the sub....
&{ $caps{$tmp} }($arg);
or
$caps{$tmp}->($arg);
> sub subONE{
> print "One recieved @_[0] \n";
-------------------------^
Don't use the '@' character for calling one single array element.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 19 Sep 2004 14:07:40 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: run a sub referenced in hash.
Message-Id: <XfqdnZpCC4JwV9DcRVn-ig@adelphia.com>
Abhinav wrote:
> nntp wrote:
>
>> I am trying to run a sub refferenced in a hash.. is there any way to do
>> this? this is what I tried.
>>
>> #!/usr/bin/perl
>>
>
> use strict;
> use warnings;
Good suggestion.
>>
>> #######################################################
>> %caps= ( red => 'subONE', blue => 'subTWO', green => 'GREEN' );
> use strict gives the error :
>
> Can't use string ("subONE") as a subroutine ref while "strict refs" in
> use at t1.pl line 10.
>
> And AFAIK, it is considered BAD practice to be doing this. (Which is why
> use strict does not allow it in the first place)
Let me clarify that. Yes, it's considered bad practice to use strings as
symbolic references. You should be storing code references in your hash
instead, like this:
my %caps = (
'red' => \&subONE,
'blue' => \&subTWO,
'green' => \&GREEN,
);
This is, in fact, the first step of creating a "dispatch table", which
is the commonly suggested alternative to symrefs. So the idea of using a
hash to store references to subroutines is a good one - the only part of
it that was a bad idea is storing the sub names as strings.
> I guess this is the right place to know more about this :
>
> perldoc perlref
More good advice!
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 7162
***************************************