Re: converting code via regex
- From: "Jerry" <jpreston@xxxxxxxxxxxxxxxxx>
- Date: Tue, 22 May 2007 09:55:24 -0500
ok!
s/then//;
my $wordCNT = 0;
my ( @Word ) = /\.(\w+)/g;
foreach my $Word ( @Word) {
my %WordList = ( "footage" => "footage",
"qty" => "qty",
"total" => "total",
"totalcost" => "totalcost",
"count" => "count",
"weight" => "weight",
"estcost" => "estcost",
"cost" => "cost",
"totwgt" => "totwgt",
"wt_SLASH_ft" => "wt_SLASH_ft",
"tothigh" => "tothigh",
"wt_SLASH_each" => "wt_SLASH_each",
"wt_SLASH_sht" => "wt_SLASH_sht",
"c" => "c",
"w" => "w",
"dimension" => "dimension",
"freight" => "freight",
"onhand" => "onhand",
"ft_oh" => "ft_oh",
"vendor" => "vendor",
"ordered" => "ordered",
"recd" => "recd",
"ln_NO_" => "ln_NO_",
"item_NO_" => "item_NO_",
"amount" => "amount",
"price" => "price",
"wgt_ea" => "wgt_ea",
"priceea" => "priceea",
"unit" => "unit",
"um" => "um",
"u_SLASH_m" => "u_SLASH_m",
"dl_NO_" => "dl_NO_",
"pt_NO_" => "pt_NO_",
"d" => "d",
"p" => "p",
"sku_NO_" => "sku_NO_",
"invitem_NO_" => "invitem_NO_",
"i" => "i",
"pt_NO_" => "pt_NO_",
"sell" => "sell",
"invqty" => "invqty",
"description" => "description",
"t" => "t",
"qtyout" => "qtyout",
"qtyout" => "qtyout",
"wgtout" => "wgtout",
"pc" => "pc",
"uofm" => "uofm",
"baldue" => "baldue",
"ptyp" => "ptyp",
"po_NO_" => "po_NO_",
"ap_NO_" => "ap_NO_",
"fob" => "fob",
"product" => "product",
"onorder" => "onorder",
"qtyin" => "qtyin",
"wgtin" => "wgtin",
"invdate" => "invdate",
"sr" => "sr",
"city" => "city",
"name" => "name",
"pt" => "pt",
);
if( $WordList{ $Word } )
{
#print "$File, $Word*$#WordList*$WordList{ $Word }\n";
#print $_ if /\"N\" \) \)/i;
s/(\w)\.\b$WordList{ $Word }\b \);/$1\.$WordList{ $Word }\[
$1.L_NO_ \] \n/g if /get/;
s/(\w)\.\b$WordList{ $Word }\b/$1\.$WordList{ $Word }\[ $1.L_NO_
\]/g if /^\s+(\w)\./ && $wordCNT == 0 && !/$WordList{ $Word }\[/i;
s/(\w)\.\b$WordList{ $Word }\b/$1\.$WordList{ $Word }\[ $1.L_NO_
\].ToString\(\)/g if /sorts/ && $wordCNT > 0;
s/(\w)\.\b$WordList{ $Word }\b/$1\.$WordList{ $Word }\[ $1.L_NO_
\].ToString\(\)/g if( /= \"/ || /!= \"/ || /== \"/ ) && !/\[/ && !/\*
\w\./g;
s/(\w)\.\b$WordList{ $Word }\b /$1\.$WordList{ $Word }\[
$1.L_NO_ \].ToString\(\) /g if( /!= \"n\"/i && $wordCNT > 0 );
#print $_ if /\"N\" \) \)/i;
s/(\w)\.\b$WordList{
$Word }\b\s+==\s+\"(\w+)\"/utils\.IsMatch\( $1\.$WordList{ $Word }\[
$1.L_NO_ \].ToString\(\), \"$2\" \)/g if !/\.ToString/ && !/\* \w\./g;
s/(\w)\.\b$WordList{
$Word }\b\s+\!=\s+\"(\w+)\"/utils\.NotMatch\( $1\.$WordList{ $Word }\[
$1.L_NO_ \].ToString\(\), \"$2\" \)/g if !/\.ToString/ && !/\* \w\./g;
# s/(\w)\.\b$WordList{ $Word }\b\s+\)/$1\.$WordList{ $Word }\[
$1.L_NO_ \].ToString\(\) \)/g if !/\),/;
s/(\w)\.\b$WordList{ $Word }\b\.ToString/$1\.$WordList{
$Word }\[ $1.L_NO_ \]\.ToString/g;
s/(\w)\.\b$WordList{ $Word }\b\s+= /$1\.$WordList{ $Word }\[
$1.L_NO_ \] = /g;
s/(\w)\.\b$WordList{ $Word }\b\s+\)/$1\.$WordList{ $Word }\[
$1.L_NO_ \] \)/g if !/\),/ && $wordCNT == 0;
s/(\w)\.\b$WordList{ $Word }\b/float.Parse\( $1\.$WordList{
$Word }\[ $1.L_NO_ \].ToString\(\) \)/g if !/(\w)\.$WordList{ $Word }\[/;
# s/(\w)\.$WordList{ $Word };/float.Parse\( $1\.$WordList{
$Word }\[ $1.L_NO_ \].ToString\(\) \);/g;
# s/(\w)\.$WordList{ $Word } /float.Parse\( $1\.$WordList{
$Word }\[ $1.L_NO_ \].ToString\(\) \) /g;
# s/(\w)\.$WordList{ $Word }$/float.Parse\( $1\.$WordList{
$Word }\[ $1.L_NO_ \].ToString\(\) \);/g;
$wordCNT++;
}
}
"Dave Weaver" <zen13097@xxxxxxxxx> wrote in message
news:465301cd$0$31824$db0fefd9@xxxxxxxxxxxxxxxxx
On Tue, 22 May 2007 08:21:23 -0500, Jerry <jpreston@xxxxxxxxxxxxxxxxx>
wrote:
I am converting some old code:^^^^^
$_ = " if (a.weight = '') and (a.w <> 'N') then"
to the following
$_ = "if ( a.weight[ a.L_NO_ ].ToString() == 0 ) and ( a.w <> 'N' )
then
How did '' become 0 ?
using
s/(\w)\.\b$WordList{ $Word }\b/$1\.$WordList{ $Word }\[ $1.L_NO_
\].ToString\(\)/g;
No need to escape most of that in the replacement part, because those
characters are only special in a regular expression (the left-hand
part of the s/// operator), not a plain string (the right-hand part).
Although I had to leave in the escaped [ otherwise perl assumes it's
part of the $WordList{} expression and tries to interpolate it.
Why does this not change the"a.w". I have a hash of key words that I
am
looping through
It works for me. If you'd shown a *short* but *complete* program that
illustrates your problem (as the posting guidelines for this group
suggest) we could have shown you what you did wrong.
#!/usr/bin/perl
use strict;
use warnings;
$_ = " if ( a.weight == 0 ) and ( a.w <> 'N' ) then";
my %WordList = ( foo => 'weight', bar => 'w' );
for my $Word ( qw(foo bar) ) {
s{(\w)\.\b$WordList{ $Word }\b}
{$1.$WordList{ $Word }\[ $1.L_NO_ ].ToString()}g;
}
print "$_\n";
Produces:
if ( a.weight[ a.L_NO_ ].ToString() == 0 ) and
( a.w[ a.L_NO_ ].ToString() <> 'N' ) then
Apart from the removal of the unneccessary back-whacks and the use of
{} as the delimiter (for better wrapping in this post), the s///
remains the same as your example.
I am looking for a finished product of
_$ = "if( ( a.weight[ a.L_NO_ ].ToString() == 0 ) && ( a.w[
a.L_NO_ ].ToString() != 'N' ) )"
What happened to the " then" at the end?
.
- Follow-Ups:
- Re: converting code via regex
- From: davedontmind@xxxxxxxxx
- Re: converting code via regex
- From: Tad McClellan
- Re: converting code via regex
- References:
- converting code via regex
- From: Jerry
- Re: converting code via regex
- From: Dave Weaver
- converting code via regex
- Prev by Date: Re: executing an sql statement in perl
- Next by Date: Re: Catching SIGALRM in a thread
- Previous by thread: Re: converting code via regex
- Next by thread: Re: converting code via regex
- Index(es):
Relevant Pages
|