Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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> Modification of a read-only value attempted at C:\JPARK\JPPERL\ + line 7. C:\JPARK\JPPERL> Modification of a read-only value attempted at C:\JPARK\JPPERL\ + line 7. C:\JPARK\JPPERL>

Replies are listed 'Best First'.
Re: Reoccuring message
by Perlbotics (Bishop) 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


    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.

Re: Reoccuring message
by SuicideJunkie (Vicar) 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 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.


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

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-11-27 12:50 GMT
Find Nodes?
    Voting Booth?