Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > TMTOWTDI

Reply
Thread Tools

TMTOWTDI

 
 
Graham Gough
Guest
Posts: n/a
 
      02-19-2004
I have been trying to solve a problem in which I wish to substitute
for *almost* all occurrences of a regular expression. For example all
occurrences of \d\d\d\d except some specific ones. I ended up with
using the e modifier and code like that given below, but had a nagging
felling that there was probably some more elegant method. Anyone any
suggestions?

Graham

$text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;

sub choose {
my $val = shift;
my $notpat = shift;
my $ret=$val;
if (!($val =~ /$notpat/)) {
$ret = "XX" . $ret . "XX";
}
return $ret;
}

 
Reply With Quote
 
 
 
 
fifo
Guest
Posts: n/a
 
      02-19-2004
At 2004-02-19 17:02 +0000, Graham Gough wrote:
> I have been trying to solve a problem in which I wish to substitute
> for *almost* all occurrences of a regular expression. For example all
> occurrences of \d\d\d\d except some specific ones. I ended up with
> using the e modifier and code like that given below, but had a nagging
> felling that there was probably some more elegant method. Anyone any
> suggestions?
>
> Graham
>
> $text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;
>
> sub choose {
> my $val = shift;
> my $notpat = shift;
> my $ret=$val;
> if (!($val =~ /$notpat/)) {
> $ret = "XX" . $ret . "XX";
> }
> return $ret;
> }
>


How about

$text =~ s/(?!1234)(\d{4})/XX$1XX/g;
 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      02-19-2004
fifo <(E-Mail Removed)> wrote:
> At 2004-02-19 17:02 +0000, Graham Gough wrote:
> > I have been trying to solve a problem in which I wish to substitute
> > for *almost* all occurrences of a regular expression. For example all
> > occurrences of \d\d\d\d except some specific ones. I ended up with

[...]
>
> How about
>
> $text =~ s/(?!1234)(\d{4})/XX$1XX/g;


Depending on the OP's data, may have undesired matches, for example, if
$text="12345"

Perhaps, if the OP only cares about 4 digit numbers, and suppose he
doesnt want to match 1234 or 6789 or 4242:
$text =~ s/(^|\D)(?!1234|6789|4242)(\d{4})(\D|$)/$1<<$2>>$3/g;

--
Glenn Jackman
NCF Sysadmin
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-19-2004
Graham Gough <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I have been trying to solve a problem in which I wish to substitute
> for *almost* all occurrences of a regular expression. For example all
> occurrences of \d\d\d\d except some specific ones. I ended up with
> using the e modifier and code like that given below, but had a nagging
> felling that there was probably some more elegant method. Anyone any
> suggestions?


Unless the exceptions can be reasonably described through a regex,
the /e modifier is bound to crop up. Nothing wrong with that.

> $text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;
>
> sub choose {
> my $val = shift;
> my $notpat = shift;
> my $ret=$val;
> if (!($val =~ /$notpat/)) {
> $ret = "XX" . $ret . "XX";
> }
> return $ret;
> }


There are a few keywords that make a Perl programmer think "Hash!", and
one of them is "a few exceptions". (Another such keyword is "unique").
In that vein, I'd go for (leaving out the obvious setup of %exceptions,
and untested)

s/(\d{4})/exists $exceptions{ $1} ? $1 : "XX$1XX"/ge;

Anno
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-20-2004
>>>>> "GJ" == Glenn Jackman <(E-Mail Removed)> writes:

>> How about
>>
>> $text =~ s/(?!1234)(\d{4})/XX$1XX/g;


GJ> Depending on the OP's data, may have undesired matches, for example, if
GJ> $text="12345"

GJ> Perhaps, if the OP only cares about 4 digit numbers, and suppose he
GJ> doesnt want to match 1234 or 6789 or 4242:
GJ> $text =~ s/(^|\D)(?!1234|6789|4242)(\d{4})(\D|$)/$1<<$2>>$3/g;

i would still go with /e but just be simpler than the OP's code.

$text =~ s/(\d{4})/ $1 eq '1234' ? $1 : "XX$1XX" /eg;

if you need to exclude many sets of 4 digits, put them in a hash:

my %keepers = map { $_ => 1 } qw( 1234 5678 9012 ) ;

$text =~ s/(\d{4})/ $keepers{$1} ? $1 : "XX$1XX" /eg;

that is clear, fast and modifiable by others.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-20-2004
Anno Siegel <(E-Mail Removed)-berlin.de> wrote:

> There are a few keywords that make a Perl programmer think "Hash!", and
> one of them is "a few exceptions". (Another such keyword is "unique").



Yet some more:

lookup
cross-reference
related to ( a "relation" )
associated with
duplicate
symbolic reference
dispatch table


Sometimes you can cut right through that thinking stuff and
head right on over to the solution:

Message-ID: <(E-Mail Removed) >

I don't know what your original problem is,
but I suggest to use a hash. --Rafael Garcia-Suarez





--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-20-2004
>>>>> "TM" == Tad McClellan <(E-Mail Removed)> writes:

TM> Anno Siegel <(E-Mail Removed)-berlin.de> wrote:
>> There are a few keywords that make a Perl programmer think "Hash!", and
>> one of them is "a few exceptions". (Another such keyword is "unique").



TM> Yet some more:

TM> lookup
TM> cross-reference
TM> related to ( a "relation" )
TM> associated with
TM> duplicate
TM> symbolic reference
TM> dispatch table

table (of any sort)
record
structure
convert
is a
set
has a
tree
mapping (or map)
dictionary
objects (most use hashes)
attributes
properties
traits
registry

this is actually an interesting list and maybe could be converted into
an FAQ: when should i use a hash?

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Graham Gough
Guest
Posts: n/a
 
      02-20-2004
Thanks for all the interesting suggestions. Like some of the posters,
my usual reaction to many programming problems is to use a hash, I
don't know why I didn't think of it this time. Old age, probably

Thanks again

Graham
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default initial array value? [TMTOWTDI] h3xx Perl Misc 5 02-22-2009 12:07 PM



Advertisments