Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^7: Pointers and References

by LanX (Saint)
on Nov 24, 2020 at 19:52 UTC ( [id://11124155]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Pointers and References
in thread Pointers and References

Lists in Perl are two things:

  • a syntax, mostly comma separated (, is a list operator)
  • a temporary stack to pass this sequence of scalars

Array is

  • a @variable holding
  • a data structure in memory

So @a = (1,2,3) means step by step

  • array variable @a at LHS forces a list assignment
  • the comma syntax denotes a list
  • the list values are pushed to a stack
  • the stack is assigned to the array and stored in memory in an AV

I hope it's clearer now.

If it's a consolation, Larry occasionally confused the terminology too, e.g. in wantarray :)

> (Modification of a read-only value attempted)

That's because 1,2,3 are literals which are read only. Use variables (sic) and you can change them in the loop. ;)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^8: Pointers and References
by bliako (Monsignor) on Nov 24, 2020 at 20:38 UTC

    yep

    use strict; use warnings; my $x = 12; foreach my $aa ($x,$x){ $aa = 13; } print $x; ---------- 13

    How about "readonly, anonymous stack" instead of list? Iam joking, enough of the nomenclature

      And also for arrays:

      Win8 Strawberry 5.8.9.5 (32) Tue 11/24/2020 17:09:57 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -l my $x = 1; my $y = 2; my @z = (3, 4, 5); $_ += 100 for @z, $x, $y; print "$x, $y, (@z)"; for ($x, @z, $y) { $_ += 100; } print "$x, $y, (@z)"; ^Z 101, 102, (103 104 105) 201, 202, (203 204 205)


      Give a man a fish:  <%-{-{-{-<

      I hope the distinction between list and arrays is clearer now, I know its confusing.

      E.g. this is a list assignment without literal list: @a = %a ,

      ... the hash is unpacked into a list which is packed into an array.

      or here you need a literal list to init an array(-ref) : $a_ref = [ 1,2,3 ]

      Please feel free to ask. :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Sure, thanks! I will ask if I have a problem.

Re^8: Pointers and References
by Leudwinus (Scribe) on Nov 25, 2020 at 15:33 UTC
    That's because 1,2,3 are literals which are read only. Use variables (sic) and you can change them in the loop. ;)

    It took me a few times to understand this line based on the examples given further below but now this makes sense!

        So $x = 42 is as useful as 1 = 42

        I don't understand this statement. I can see where an expression like $x = 42 might be useful, but 1 = 42 can't possibly be useful because it won't compile.

        Update: Or do you mean "if $x is aliased to a literal value (e.g., 1), $x = 42 is equivalent to 1 = 42"? (Update: And specifically WRT to the for-loop here? I didn't look back far enough. :)


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124155]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found