Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

converting code via regex


HI!

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

using

   s/(\w)\.\b$WordList{ $Word }\b/$1\.$WordList{ $Word }\[ $1.L_NO_
\].ToString\(\)/g;

   Why does this not change the"a.w".  I have a hash of key words that I am
looping through

I am looking for a finished product of

_$ = "if( ( a.weight[ a.L_NO_ ].ToString() == 0 ) && ( a.w[
a.L_NO_ ].ToString() != 'N' ) )"

Thanks,

Jerry

On Tue, 22 May 2007 08:21:23 -0500, Jerry <jpres@general-steel.com> 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?
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" <zen13@zen.co.uk> wrote in message

news:465301cd$0$31824$db0fefd9@news.zen.co.uk...

Jerry <jpres@general-steel.com> wrote:
>       s/then//;

You want "authenticate" to become "auticate" ?

--
    Tad McClellan                          SGML consulting
    t@augustmail.com                   Perl programming
    Fort Worth, Texas

Please don't top-post. If you're replying to someone, trim their
message to
something small enough to provide context for your post, and put your
replies
*below* the relevant parts of the post you're replying to.

Jerry <jpres@general-steel.com> wrote:
>         my $wordCNT = 0;
>         my ( @Word ) = /\.(\w+)/g;

...

That is not a *short* but *complete* program that exhibits your
problem.
When I run it it outputs nothing. What did you expect?

>  foreach my $Word ( @Word) {

>    my %WordList = (  "footage"       => "footage",
>                      "qty"           => "qty",
>                      "total"         => "total",
>                #  ... snip words ...
>                      "pt"            => "pt",
>                   );

So the only point of %WordList is to see if a word in the original $_
is
'valid' ?  There's no need to initialize %WordList each time through
the loop,
and there are less verbose ways to initialize it:

    my %WordList = map { $_ => 1 } qw(footage qty total ... pt);
    for my $Word ( /\.(\w+)/g ) {
        ...
    }

>   if( $WordList{ $Word } )
>   {

>   s/(\w)\.\b$WordList{ $Word }\b \);/$1\.$WordList{ $Word }\[ $1.L_NO_ \] \n/g if /get/;

By the very nature of your code, if you get here $WordList{$Word} will
always
be the same as $Word, so why use the more verbose form?

    s/(\w)\.\b$Word\b \);/$1\.$Word\[ $1.L_NO_ \] \n/g if /get/;

Also (as I mentioned in my previous post) you don't need all those
back-slashes
in the right-hand side of the s///: So that line can become:

    s/(\w)\.\b$Word\b \);/$1.${Word}[ $1.L_NO_ ] \n/g if /get/;

The \b's in the left-hand side are redundant too, given your word list
above:

    s/(\w)\.$Word \);/$1.${Word}[ $1.L_NO_ ] \n/g if /get/;

There - that's a little more readable than your original line.

>  s/(\w)\.\b$WordList{ $Word }\b/$1\.$WordList{ $Word }\[ $1.L_NO_ \]
>           ToString\(\)/g if( /= \"/ || /!= \"/ || /== \"/ ) && !/\[/ && !/\*\w\./g;

What's the "/g" on the last pattern match for?

  if( /= \"/ || /!= \"/ || /== \"/ )

is exactly the same as:

   if /= "/

because if your string contains '!= "' or '== "' it will still match
the first
pattern.  And double quotes don't need to be escaped in a regex.

I find the "and" "or" and "not" operators to be much more readable
than "&&"
"||" and "!":

    ... if /= "/ and not /\[/ and not /\*\w\./;

>   s/(\w)\.\b$WordList{ $Word }\b /$1\.$WordList{ $Word }\[ $1.L_NO_ \]
>                       ToString\(\) /g if( /!= \"n\"/i && $wordCNT > 0 );

    s/(\w)\.$Word /$1.${Word}[ $1.L_NO_ ].ToString() /g if /!= "n"/i
and $wordCNT > 0;

If you're still having problems, post a *short* but *complete* program
(which
uses 'strict' and 'warnings' and includes some sample data) that
demonstrates
your problem. Tell us what output you see, and what you're expecting
to see.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc