Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

search, replace and backrefrences

by Helter (Chaplain)
on Sep 30, 2002 at 15:13 UTC ( #201734=perlquestion: print w/replies, xml ) Need Help??

Helter has asked for the wisdom of the Perl Monks concerning the following question:

I found a solution that works, but to gain more understanding (and I'm stubborn that way) I want to know why my first solution does not work.

I'm parsing a file, looking for a single line to replace 2 characters.
Line in question:
__msr2f6: data8 0b00000000000000000000000000_1_0_000000000000001 +0101_00000000000000000
My initial search/replace was this:
$line =~ s/^(__msr2f6:\s+data8 0b[01]+_)[01]_[01](_[01]+_[01]+)/$10_0$ +2/;
This causes an error to be printed:
Use of uninitialized value in concatenation (.) at myscript.pl line 33 +, <FILE> line 915.
A co-worker suggested a change, but didn't know why it might work:
$line =~ s/^(__msr2f6:\s+data8 0b[01]+)_[01]_[01]_([01]+_[01]+)/$1_0_0 +_$2/;
So I moved the leading and trailing underscore out of the parens and put them in the replace string.

Here are some things I think I understand, but then again maybe not...

1. Backrefrences are 1-9, only a single digit.
2. Underscores are valid variable name components.

So what changes between these two examples? If the first variable is being treated as $10_0, then why is the second not being treated as $1_0_0_?

This solution also works:
$line =~ s/^(__msr2f6:\s+data8 0b[01]+_)[01]_[01](_[01]+_[01]+)/$1."0_ +0".$2/e;
But it is probably slower than the other working solution.

Thanks for the enlightenment!

Replies are listed 'Best First'.
Re: search, replace and backrefrences
by diotalevi (Canon) on Sep 30, 2002 at 15:18 UTC

    I suspect perl is DWIMing your potential $1_0_0_ into $1. The actual fix is to just encose the '1' in curlies like s/ ... /${1}0_0$2/. You use curlies to disambiguate interpolated variables where needed.

Re: search, replace and backrefrences
by demerphq (Chancellor) on Sep 30, 2002 at 15:27 UTC
    Well, im not real sure that $1_0_0_ isnt being treated as such.

    But a useful trick in this situation and others is to use the form ${varname} to delimit the the identifier. Ie

    my $foo="Groove"; print "$food"; # Error $food is not a declared indentifier. print "${foo}d"; # same as $foo."d";
    So the rhs of the first and second could be ${1}0_0$2 and ${1}_0_0_$2 respectively.

    --- demerphq
    my friends call me, usually because I'm late....

      Well, im not real sure that $1_0_0_ isnt being treated as such.

      Variable names that start with a digit may only have
      more digits in the name.

      This seems a murky area. my $99; throws an error.

      This post has little to do with the original question.

        Even with the number formatting semantics where 1_000_000 == 1000000?

        Variable names that start with a digit may only have more digits in the name.

        Cool. I didnt know that. What perldoc is it in?

        This seems a murky area. my $99; throws an error.

        Well, i agree its weird, but its not that murky. All of the ^\d+$ variables are global only and cannot be my()ed (and all of the scalars are read only).

        --- demerphq
        my friends call me, usually because I'm late....

Re: search, replace and backrefrences
by Helter (Chaplain) on Sep 30, 2002 at 17:46 UTC
    After some more reading I found that my first assumption is only half true.
    $1-$9 are ALWAYS backrefrences, where $10 and up are only backrefrences if more than 10 parens were used.
    This mixes with the comments about a variable that starts with a digit, can only have digits.

    So $10 is taken as the whole name of a variable (undefined)
    $1_ gets treated like $1 because _ is not a digit.

    Thanks for all the help, I'm slowly understanding the subtle nature of Perl :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://201734]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2021-12-03 01:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (27 votes). Check out past polls.

    Notices?