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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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!

In reply to search, replace and backrefrences by Helter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-05-22 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (80 votes). Check out past polls.

    Notices?