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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.