Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Sometimes it's in Void Context

by dws (Chancellor)
on May 31, 2002 at 23:07 UTC ( [id://170851]=note: print w/replies, xml ) Need Help??


in reply to Sometimes it's in Void Context

Ah, but you are dealing with a slice, once you dereference the reference. Try   print @{$lines[0]}[0,1,2]; As to why you're seeing the behavior you are... uh... uh...

Update: Oh bother. I misunderstood the meta-problem.

Replies are listed 'Best First'.
Re: Re: Sometimes it's in Void Context
by Ovid (Cardinal) on May 31, 2002 at 23:32 UTC

    Heh :) Even though your answering a totally different question, it does raise another issue. Why are the following three cases treated differently?

    $lines[0] = [ 3,2,1 ]; print $lines[0][ 3,2,1 ]; print @{$lines[0]}[ 3,2,1 ];

    Ignoring, for the moment, the "Useless use of constant in void context" warnings, how does Perl know that the first use of [ 3,2,1 ] is to generate a reference to an anonymous array from a list, while the second example doesn't reduce to this:

    print $lines[ARRAY(0x1a7efd8)];

    We have the same issue with the third example recognizing that [ 3,2,1 ] is to be used as a list of array indices for the slice.

    This behavior should is difficult to keep straight:

    $lines[0] = [ "dog", "cat" ];

    Why doesn't that just assign "cat" to the first element of @lines? My assumption is that it is because the square brackets are performing a subtle double-duty. In this example:

    print $lines[0][0,1];

    the square brackets are being used to indicate an array element. In another example:

    $lines[0] = [ "dog", "cat" ];

    the square brackets are being used to create a reference to an anonymous array and constructs the anonymous array from a list. Keeping things straight can be a major headache, which is why I'm glad Perl does it for me :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Okay, lemme know if I'm wrong here, but:
      $lines[0] = [ 3,2,1 ];
      The brackets are forming an array context. You're assigning an array to the array member
      print $lines[0][ 3,2,1 ];
      I am assuming what is going here is that the array dereference is looking for a numerical value to figure out where to look in the array. You get the same error here:
      print scalar(4,5); jaybonci@starlite:~/perl/pm$ ./array.pl Useless use of a constant in void context at ./array.pl line 6. 5jaybonci@starlite:~/perl/pm$
      Looking at the comma operator in the perlop page, this makes sense. The values are evaluated one by one.

      It wouldn't make sense for perl to be looking for an array in a dereference operator, because you need a numerical value. Why would it map those values or try to return a sub array based on those values. The execution order says to resolve the commas first.

      Like I said, I could be way off.

          --jb

        You misunderstood the question, just like dws did. I didn't get it until just a few moments ago either. Ovid's question is not "why does this not work as initially expected", nor "why do I get this warning", it is "why do I get the warning sometimes".

        In your example, if you change print scalar(4,5); to print scalar(0,1);, you will get no warnings. His question is why and when that happens.

        As chromatic quotes the perl source, that's the case because the warning is kludged not to trigger for the two values 0 and 1.

        ____________
        Makeshifts last the longest.
      Why are the following three cases treated differently?

      I suspect it is because the parser expects different things at the different times. Look at the first one for example, it is pretty reasonable for this to be a 3-element array, we often make such assignments. In fact, for us to be able to make such assignments, this has to be a three element array.

      In the second case the parser is looking for a subscript instead of another array, so it tries to make what it finds into one.

      HTH, --traveler

      I don't see the issue here, sorry.

      $lines[0] = [3,2,1]: here we agree that [3,2,1] must mean an array reference.

      $lines[0][3,2,1]: I fail to see how this can parse as something like ``$lines[ARRAY(0x1a7efd8)]'' (i.e. [3,2,1] becomes an array reference, which is "numericalised" into the value 0x1a7efd8. For that, you'd need an extra set of square brackets: the inner pair delimits an array reference, the outer pair delimits an array index! Indeed, testing $lines[0][[3,2,1]] shows that it behaves as a numericalised array reference.

      I can see nowhere in the "grammar" of Perl (I use the term loosely...) where both a scalar value (such as an array reference!) and an array subscript are legal. So there's no problem here.

        (BTW, formaly speaking, perl /almost/ has a grammar: look at perly.y in the source tree.)


        We are using here a powerful strategy of synthesis: wishful thinking. -- The Wizard Book

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-28 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found