Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Reoccuring message

by truthseeker66 (Novice)
on Oct 04, 2012 at 18:59 UTC ( #997292=perlquestion: print w/ replies, xml ) Need Help??
truthseeker66 has asked for the wisdom of the Perl Monks concerning the following question:

# My Code in the file.
#!usr/bin/perl use warnings; @array = (1 .. 10); for ($1 = 0; $i <= 10; $i++) { $element = $array[$i]; print "Element $1 equals $element \n"; }
# The output of executing the above file.
C:\JPARK\JPPERL>test.pl Modification of a read-only value attempted at C:\JPARK\JPPERL\test.pl + line 7. C:\JPARK\JPPERL>test.pl Modification of a read-only value attempted at C:\JPARK\JPPERL\test.pl + line 7. C:\JPARK\JPPERL>

Comment on Reoccuring message
Select or Download Code
Re: Reoccuring message
by SuicideJunkie (Priest) on Oct 04, 2012 at 19:03 UTC

    Read the ENTIRE message. It will tell you what line number to look at.

    If you're still not sure how you messed up, you'll need to trim down your code to the minimum amount that still shows your problem. If it is still not obvious, post that code along with the exact error message (copy paste, don't type from memory).

    Alternatively, build more pylons and recruit some psychics.

    EDIT: That's a bit better, but use <code></code> tags so that your code & results are legible

Re: Reoccuring message
by Perlbotics (Abbot) on Oct 04, 2012 at 19:21 UTC
    Try to replace all occurrences of $1 with $i. The problem occurs when assigning 0 to $1 in the for()-loop construct.

    You cannot assign to $1 directly using the = operator - so from that point of view, $1 is read-only. $1 is usually set by a matching regular expression (capture), see also perlre.

    Maybe, you should also increase your editors font size? ;-)

    Update: Off-topic but maybe of interest: Announcing Source Code Pro

Re: Reoccuring message
by Kenosis (Priest) on Oct 04, 2012 at 19:29 UTC

    Always:

    use strict; use warnings;

    Also, use lexically-scoped variables, e.g.:

    my @array ... my $i ... my $element ...

    And I think you may have meant the following:

    $i < 10 $array[$i]

    Hope this helps!

      Now I see CR LF at the end of every line in Notepad++. I didn't see this before and suddenly it appeared. Could you tell me how to remove this? #/usr/bin/perl CR LF . . . . Thank you,

        Hi truthseeker66,

        Not exactly a Perl question (I think), but in Notepad++, there should be a paragraph icon in the toolbar with a tooltip of "Show All Characters". Making sure that button is "out" (unpressed? what's the real term?) should make the CR/LF characters not show.

        Otherwise, going to View / Show Symbol / Show All Characters should do the same thing as the toolbar button - just make sure that menu item is not checked.

        Enjoy,
Re: Reoccuring message
by Bloodnok (Vicar) on Jun 09, 2014 at 12:06 UTC
    Although use strict; is highly recommended (almost to the point of mandated:-) by monks far more knowledgeable than me, in this case, the behaviour would be identical to that observed - which, IMHO, might provide a use case for the improvement of perl itself by way of identifying the variable that it considers to be read-only within the error message - sometimes I've struggled to try and spot problems such as this.

    Anyway, in for ($1 = 0; $i <= 10; $i++) {, you assign a value of 0 to $1, methinx that you meant to write for ($i = 0; $i <= 10; $i++) {, or even for (my $i = 0; $i <= 10; $i++) {.

    I would also point out that, strictly speaking, the message isn't Reoccuring since it only occurs once per run.

    Update:

    Thanx to AnomalousMonk (I do like like name:-), added my to 2nd suggestion.

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997292]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-07-10 05:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (199 votes), past polls