Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day rmarkman,

Welcome to the Monastery.

Update (strike): I looked up the error message and made an incorrect inference. I suspect I read "off the end of the string" as "off the end of the array". Anyway, regardless of the reason, what I wrote is wrong and is now stricken.

You're getting the error because you're attempting to assign a value to an element past the end of the array. If you search for "Modification of a read-only value attempted" in "perldiag -- Perl diagnostic messages" you'll get a lot of information about this. However, the opening sentence sums up yor problem:

"You tried, directly or indirectly, to change the value of a constant."

Your split statement, as written, only produces 6 elements: ('A' .. 'F'). You can print $#flds or scalar(@flds) to confirm this.

When you add a LIMIT of -1, you get 10 elements. This behaviour is documented in split: there's examples roughly in the middle of the page.

With the code as posted, both $flds[16] and $flds[8] are undefined. Attempting to assign undef doesn't cause an issue:

$ perl -we 'undef=undef' $

Attempting to assign 0 generates the error you're getting:

$ perl -we 'undef=0' Modification of a read-only value attempted at -e line 1. $

If you use split like this:

@flds = split(/(\t)/, $value);

You'll get 18 elements. That's also documented (at the end of the page).

— Ken


In reply to Re: split problem by kcott
in thread split problem by rmarkman

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 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?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-26 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found