[30621] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1866 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Sep 21 09:09:46 2008

Date: Sun, 21 Sep 2008 06:09:06 -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           Sun, 21 Sep 2008     Volume: 11 Number: 1866

Today's topics:
    Re: Easiest way to do this? <pilcrow6@gmail.com>
    Re: Regular express for <p>, <ul> and <ol> tags <tadmc@seesig.invalid>
    Re: Regular express for <p>, <ul> and <ol> tags <jurgenex@hotmail.com>
        Replace all occurences of a char, except the first <bart@nijlen.com>
    Re: Replace all occurences of a char, except the first <someone@example.com>
    Re: Replace all occurences of a char, except the first <alex@digriz.org.uk>
    Re: Replace all occurences of a char, except the first <tim@burlyhost.com>
    Re: Replace all occurences of a char, except the first <tadmc@seesig.invalid>
    Re: Replace all occurences of a char, except the first <ben@morrow.me.uk>
    Re: Replace all occurences of a char, except the first <someone@example.com>
    Re: Replace all occurences of a char, except the first <tim@burlyhost.com>
        The Perl language in general <roseeea@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 20 Sep 2008 23:21:05 -0700
From: Pilcrow <pilcrow6@gmail.com>
Subject: Re: Easiest way to do this?
Message-Id: <o6pbd4t6ts73g9atifbctt7ke9hf67thfq@4ax.com>

On Fri, 19 Sep 2008 10:13:29 -0700 (PDT), "void.no.spam.com@gmail.com"
<void.no.spam.com@gmail.com> wrote:

>On Sep 19, 9:15 am, hymie_@_lactose.homelinux.net (hymie!) wrote:
>> In our last episode, the evil Dr. Lacto had captured our hero,
>>   <void.no.spam....@gmail.com>, who said:
>>
>> >I need to be able to take any dollar amount, such as 25103.34, and
>> >multiply it by 0.0000056.  
>> >Then I need to take the result and truncate
>> >everything after the fifth decimal place, so if the result is
>> >0.140578704, then I will have 0.14057.
>>
>> This seems like a useless step considering
>
>Thanks for the answer.  That step actually isn't useless, because if
>the multiplication results in something like 0.140000001, and then you
>don't truncate, the final result will be 0.15 when it should be 0.14.
>

you *did* say 'ceiling function'?  So ceil(0.14) would be 0.15, *if*
perl had a ceiling function.

So, here it is:

-----------------------------------------------------------------
use strict; use warnings;
while(<DATA>){
    last if /^$/;
    chomp;
    printf "%15s -> %.2f\n", $_, $_ * 0.0000056 + 0.005;
}
__DATA__
25103.34
0.01
1.56
345.12
1234567.89
9876543.21
789012345678.90


-----------------------------------------------------------------
OUTPUT:

       25103.34 -> 0.15
           0.01 -> 0.01
           1.56 -> 0.01
         345.12 -> 0.01
     1234567.89 -> 6.92
     9876543.21 -> 55.31
789012345678.90 -> 4418469.14
 


------------------------------

Date: Sat, 20 Sep 2008 16:12:07 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Regular express for <p>, <ul> and <ol> tags
Message-Id: <slrngdapp7.4qf.tadmc@tadmc30.sbcglobal.net>

sln@netherlands.com <sln@netherlands.com> wrote:
> On Fri, 19 Sep 2008 21:36:35 -0500, Tad J McClellan <tadmc@seesig.invalid> wrote:
>
>>sln@netherlands.com <sln@netherlands.com> wrote:
>>> On Mon, 25 Aug 2008 16:47:42 GMT, Jürgen Exner <jurgenex@hotmail.com> wrote:
>>>
>>>>Shahid <mirzashahidmahmood@gmail.com> wrote:
>>>>>I am parsing an .HTML file
>>
>>
>>>>HTML is not a regular language!
>>>
>>> Not only not a real language, 
>>
>>
>>He did not say real language.
>>
>>He said regular language.
>>
>>    http://en.wikipedia.org/wiki/Regular_language
>>
>>
>>>> While the extensions to Perl's RE
>>>>language might be powerful enough to cover HTML
>>>
>>> That power is not needed,
>>
>>
>>But it is. It has been mathematically proven to be needed.
>>
>>If a parser can accept a context-free language (eg. HTML) then
>>the parser is not "regular".
>>
>>
>>>>, no sane person would
>>>>even try to do so.
>>>
>>> I guess I'm not sane then
>>
>>
>>You just don't know enough about language theory to be taken seriously.
>>
>>Should you choose to remedy that, you could start with:
>>
>>    http://en.wikipedia.org/wiki/Chomsky_hierarchy#The_hierarchy
>>
>>If you should really want to sling the lingo believably, then
>>continue with the "Dragon book".
>
> My friends would call you an "utter, and complete idiot", but I won't.


Then your friends don't know enough about language theory to be 
taken seriously either.


> I'm releasing a version 2 of my stuff.


Post it to rec.humor.funny where it belongs.


> You live in a fantacy world, and have no knowledge of what markup is.


Right. 

The XML FAQ accepts lots of answers from people who have no knowledge.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Sun, 21 Sep 2008 04:42:42 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Regular express for <p>, <ul> and <ol> tags
Message-Id: <o9acd4llbosjcadovfja0nj88778pl8593@4ax.com>

sln@netherlands.com wrote:
>My friends would call you an "utter, and complete idiot", but I won't.

I don't need imaginary friends, I simply do call you ignorant. That in
itself can be changed if a person is willing to learn. Unfortunately I
don't see any signs of that happening.

>I'm releasing a version 2 of my stuff. And its loaded. You would have
>to write Perl interface just to simplify all the low level tools added.

So what?

>The engine has been re-written. 

So what?

>It does any markup. 

Great for you. But what does that have to do with context-sensitive
languages and regular parsers?

>Traps errors better than,
>well... better than any parser that ever was. 

So what?

>Parsing markup is the easiest thing in
>the world to do. 

Maybe, but so what?

>Trapping errors isin't so easy. My Perl regular expression engine is the
>best in the world, its better than C based code, much better.

Then maybe you can submit it to replace the existing RE engine in perl?

>You live in a fantacy world, and have no knowledge of what markup is. Only in the vaguest
>sence.
>
>Its not Parsing I am promoting, my power is in my parsing. I am promoting my tools and
>nothing else.

So what?

>And I have some very powerful tools I am introducing in version 2.

So what?

>WHAT, YOU WANT TO PARSE HTML WITH THE SIMPLEST REGULAR EXPRESSION ???????
>Simple, and stupid, and the most basic!!!!

Not possible. As has been _PROVEN_ in the past 4 decades many times over
you cannot parse a context-sensitive language with regular expressions. 
If a language can be parsed with REs then it is a regular language. If
it is not a regular language, then it cannot be parsed with RE. 
That's where the name regular expression originates. 

If you want to disprove Chomsky then please go ahead. If you succeed
then the Turing Award is within your reach.

>I was hoping not to have to do this. There unique examples of the power in the 
>tools when I post version 2. Posted below is just simple regular expression.

And where is the computational logic that implements the
context-sensitive part of the parser? 

>Why do you even try to test me? What are you afraid of?
>
>sln/aka ROBIC0

robic0, robic0, .... somehow that rings a bell. Indeed, there was that
troll who infested this NG a few years ago:
http://groups.google.com/groups?as_q=robic0+and+PLONK&as_ugroup=comp.lang.perl.misc

I thought I smelled the same stench of clueless ignorance again.

*PLONK*

jue


------------------------------

Date: Sat, 20 Sep 2008 00:30:23 -0700 (PDT)
From: Bart Van der Donck <bart@nijlen.com>
Subject: Replace all occurences of a char, except the first
Message-Id: <070638ee-6f47-4cab-a48d-187264c2e927@34g2000hsh.googlegroups.com>

Hello,

I got intrigued by the following challenge which seems to be
impossible.

Is it possible to write a regular expression that replaces all
occurences of a character except the first occurence ?

  a''bc'def'g' -> a'bcdefg
  '''ab'cd'efg -> 'abcdefg
  abc'd'e''f'g -> abc'defg
  etc.

--
 Bart


------------------------------

Date: Sat, 20 Sep 2008 04:40:33 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: Replace all occurences of a char, except the first
Message-Id: <Ri5Bk.25576$PK.14078@newsfe04.iad>

Bart Van der Donck wrote:
> 
> I got intrigued by the following challenge which seems to be
> impossible.
> 
> Is it possible to write a regular expression that replaces all
> occurences of a character except the first occurence ?
> 
>   a''bc'def'g' -> a'bcdefg
>   '''ab'cd'efg -> 'abcdefg
>   abc'd'e''f'g -> abc'defg
>   etc.

$ perl -e'
my @x = ( "a##bc#def#g#", "###ab#cd#efg", "abc#d#e##f#g" );
for ( @x ) {
     print;
     my $count;
     s/(#)/ $count++ ? "" : $1 /eg;
     print " -> $_\n";
     }
'
a##bc#def#g# -> a#bcdefg
###ab#cd#efg -> #abcdefg
abc#d#e##f#g -> abc#defg




John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


------------------------------

Date: Sat, 20 Sep 2008 16:47:48 +0100
From: Alexander Clouter <alex@digriz.org.uk>
Subject: Re: Replace all occurences of a char, except the first
Message-Id: <4iifq5-pug.ln1@woodchuck.wormnet.eu>

Hi,

John W. Krahn <someone@example.com> wrote:
> Bart Van der Donck wrote:
>> 
>> I got intrigued by the following challenge which seems to be
>> impossible.
>> 
>> Is it possible to write a regular expression that replaces all
>> occurences of a character except the first occurence ?
>> 
>>   a''bc'def'g' -> a'bcdefg
>>   '''ab'cd'efg -> 'abcdefg
>>   abc'd'e''f'g -> abc'defg
>>   etc.
> 
> $ perl -e'
> my @x = ( "a##bc#def#g#", "###ab#cd#efg", "abc#d#e##f#g" );
> for ( @x ) {
>     print;
>     my $count;
>     s/(#)/ $count++ ? "" : $1 /eg;
>     print " -> $_\n";
>     }
> '
> a##bc#def#g# -> a#bcdefg
> ###ab#cd#efg -> #abcdefg
> abc#d#e##f#g -> abc#defg
> 
Well I just learnt something from 'man perlretut', "Looking ahead and looking behind":

$ perl -e '
my @x = ( "a##bc#def#g#", "###ab#cd#efg", "abc#d#e##f#g" );

for ( @x ) {
  print;

  s/(?<=#)(.*?)#/$1/g;

  print " -> $_\n";
}
'
a##bc#def#g# -> a#bcdefg
###ab#cd#efg -> #abcdefg
abc#d#e##f#g -> abc#defg

Cheers

Alex


------------------------------

Date: Sat, 20 Sep 2008 13:32:18 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Replace all occurences of a char, except the first
Message-Id: <m5dBk.25755$PK.14840@newsfe04.iad>

Bart Van der Donck wrote:

> Hello,
> 
> I got intrigued by the following challenge which seems to be
> impossible.
> 
> Is it possible to write a regular expression that replaces all
> occurences of a character except the first occurence ?
> 
>   a''bc'def'g' -> a'bcdefg
>   '''ab'cd'efg -> 'abcdefg
>   abc'd'e''f'g -> abc'defg
>   etc.
> 
> --
>  Bart

I assume you mean that it'll have to locate any character that's
repeated (not a predetermined one) and only remove those additional
occurrences, and only use a regular expression?

I'd use a different method to accomplish the task, but it is indeed
interesting to try and do this only with a regex.

The following should work and only remove the second or higher instance
of any character (so you a string with "'''sfsf'et'st464y'''" will
result in "'sfet46y".  Pretty cool, eh?

Here's the regex logic:

$string =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;

Here's an example based on your strings, using only a regular
expression:

script.pl:

!/usr/bin/perl
use warnings;
use strict;

my $linea = "a''bc'def'g'";
my $lineb = "'''ab'cd'efg";
my $linec = "abc'd'e''f'g";

print "$linea -> ";
$linea =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;
print "$linea\n";

print "$lineb -> ";
$lineb =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;
print "$lineb\n";

print "$linec -> ";
$linec =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;
print "$linec\n";


The output:
~]$ ./script.pl
a''bc'def'g' -> a'bcdefg
'''ab'cd'efg -> 'abcdefg
abc'd'e''f'g -> abc'defg

Is that what you were trying to do?
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


------------------------------

Date: Sat, 20 Sep 2008 16:14:48 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Replace all occurences of a char, except the first
Message-Id: <slrngdapu8.4qf.tadmc@tadmc30.sbcglobal.net>

Bart Van der Donck <bart@nijlen.com> wrote:

> I got intrigued by the following challenge which seems to be
> impossible.
>
> Is it possible to write a regular expression that replaces all
> occurences of a character except the first occurence ?


No, it is not possible to replace anything with only a regular expression.

A regular expression either "matches" or "does not match", it cannot "replace".

An operator that uses a regular expression as on of its operands,
such as s/// can probably do that without much trouble though.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Sat, 20 Sep 2008 22:34:30 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Replace all occurences of a char, except the first
Message-Id: <6s6gq5-ucb.ln1@osiris.mauzo.dyndns.org>


Quoth Tim Greer <tim@burlyhost.com>:
> Bart Van der Donck wrote:
> 
> > Is it possible to write a regular expression that replaces all
> > occurences of a character except the first occurence ?
> > 
> >   a''bc'def'g' -> a'bcdefg
> >   '''ab'cd'efg -> 'abcdefg
> >   abc'd'e''f'g -> abc'defg
> >   etc.
> 
> I assume you mean that it'll have to locate any character that's
> repeated (not a predetermined one) and only remove those additional
> occurrences, and only use a regular expression?
> 
> I'd use a different method to accomplish the task, but it is indeed
> interesting to try and do this only with a regex.
> 
> The following should work and only remove the second or higher instance
> of any character (so you a string with "'''sfsf'et'st464y'''" will
> result in "'sfet46y".  Pretty cool, eh?
> 
> Here's the regex logic:
> 
> $string =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;

Without using /e:

    ~% perl -le'$_ = "abccbdcdc"; 1 while s/(.)(.*)\1/$1$2/g; print'
    abcd

Using 5.10's \K we can remove the replacement part:

    ~% perl5.10.0 -le'$_="abccbdcdc"; 1 while s/(.).*\K\1//g; print'
    abcd

and if we reverse the string before and after (so we can use look*ahead*
instead, which can be variable-length) we can remove the while loop:

    ~% perl -le'$_ = reverse "abccbdcdc"; s/(.)(?=.*\1)//g; 
        print scalar reverse' 
    abcd

Ben

-- 
   Although few may originate a policy, we are all able to judge it.
                                               Pericles of Athens, c.430 B.C.
  ben@morrow.me.uk


------------------------------

Date: Sat, 20 Sep 2008 14:49:08 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: Replace all occurences of a char, except the first
Message-Id: <pdeBk.634$tp1.466@newsfe06.iad>

Bart Van der Donck wrote:
> 
> I got intrigued by the following challenge which seems to be
> impossible.
> 
> Is it possible to write a regular expression that replaces all
> occurences of a character except the first occurence ?
> 
>   a''bc'def'g' -> a'bcdefg
>   '''ab'cd'efg -> 'abcdefg
>   abc'd'e''f'g -> abc'defg
>   etc.

$ perl -e'
my @x = ( "a##bc#def#g#", "###ab#cd#efg", "abc#d#e##f#g" );
for ( @x ) {
     print;
     /#/ && substr( $_, $+[0] ) =~ tr/#//d;
     print " -> $_\n";
     }
'
a##bc#def#g# -> a#bcdefg
###ab#cd#efg -> #abcdefg
abc#d#e##f#g -> abc#defg



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


------------------------------

Date: Sat, 20 Sep 2008 14:55:57 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Replace all occurences of a char, except the first
Message-Id: <OjeBk.635$tp1.385@newsfe06.iad>

Ben Morrow wrote:

> 
> Quoth Tim Greer <tim@burlyhost.com>:
>> Bart Van der Donck wrote:
>> 
>> > Is it possible to write a regular expression that replaces all
>> > occurences of a character except the first occurence ?
>> > 
>> >   a''bc'def'g' -> a'bcdefg
>> >   '''ab'cd'efg -> 'abcdefg
>> >   abc'd'e''f'g -> abc'defg
>> >   etc.
>> 
>> I assume you mean that it'll have to locate any character that's
>> repeated (not a predetermined one) and only remove those additional
>> occurrences, and only use a regular expression?
>> 
>> I'd use a different method to accomplish the task, but it is indeed
>> interesting to try and do this only with a regex.
>> 
>> The following should work and only remove the second or higher
>> instance of any character (so you a string with
>> "'''sfsf'et'st464y'''" will
>> result in "'sfet46y".  Pretty cool, eh?
>> 
>> Here's the regex logic:
>> 
>> $string =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;
> 
> Without using /e:
> 
>     ~% perl -le'$_ = "abccbdcdc"; 1 while s/(.)(.*)\1/$1$2/g; print'
>     abcd
> 
> Using 5.10's \K we can remove the replacement part:
> 
>     ~% perl5.10.0 -le'$_="abccbdcdc"; 1 while s/(.).*\K\1//g; print'
>     abcd
> 
> and if we reverse the string before and after (so we can use
> look*ahead* instead, which can be variable-length) we can remove the
> while loop:
> 
>     ~% perl -le'$_ = reverse "abccbdcdc"; s/(.)(?=.*\1)//g;
>         print scalar reverse'
>     abcd
> 
> Ben
> 

This is why I enjoy Perl.  There's usually several ways of doing it.  I
was purposely trying to be quirky, but in all seriousness for the views
of this thread, Ben's suggestion above is better (more efficient than
using my solution with $` -- it was all in good fun though).  I wasn't
familiar with \K (I'm still in 5.8.x), so that's pretty cool.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


------------------------------

Date: Sat, 20 Sep 2008 02:08:09 -0700 (PDT)
From: ROSEEE <roseeea@gmail.com>
Subject: The Perl language in general
Message-Id: <657ea284-78d6-4fef-ba49-a16e5228a834@b30g2000prf.googlegroups.com>

http://perllanguage.blogspot.com


------------------------------

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 V11 Issue 1866
***************************************


home help back first fref pref prev next nref lref last post