Do you know where your variables are? | |
PerlMonks |
Re: split problemby kcott (Archbishop) |
on Nov 29, 2016 at 23:12 UTC ( [id://1176871]=note: 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:
Attempting to assign 0 generates the error you're getting:
If you use split like this:
You'll get 18 elements. That's also documented (at the end of the page). — Ken
In Section
Seekers of Perl Wisdom
|
|