in reply to split problem
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: split problem
by kennethk (Abbot) on Nov 29, 2016 at 23:23 UTC | |
by kcott (Archbishop) on Nov 30, 2016 at 00:09 UTC | |
by Anonymous Monk on Nov 30, 2016 at 01:03 UTC | |
Re^2: split problem
by stevieb (Canon) on Nov 29, 2016 at 23:16 UTC | |
by Anonymous Monk on Nov 29, 2016 at 23:32 UTC | |
by kcott (Archbishop) on Nov 30, 2016 at 00:17 UTC | |
Re^2: split problem
by Anonymous Monk on Nov 29, 2016 at 23:25 UTC | |
by kcott (Archbishop) on Nov 29, 2016 at 23:58 UTC | |
by stevieb (Canon) on Nov 29, 2016 at 23:30 UTC |