Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^3: Using 'keys' on a list

by choroba (Archbishop)
on Jun 29, 2021 at 15:13 UTC ( #11134452=note: print w/replies, xml ) Need Help??

in reply to Re^2: Using 'keys' on a list
in thread Using 'keys' on a list

> Do you know why this might be?

Lists don't have keys. For arrays, keys returns the indices of the elements, not every second element. So, given a list, should keys use the hash semantics or the array one?

Also note that a "list" in fact doesn't exist in Perl. There's a list context, but there always has to be something in the context, and this something is never a list. It might be a hash, it might be an array, or a sequence of comma operators. But keys doesn't want to see the hash (or array) in a list context, it wants to see its underlying HV or AV (which also makes getting the keys much faster, Perl doesn't really iterate over alternating keys and values, skipping the latter).

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^4: Using 'keys' on a list
by lammey (Novice) on Jun 29, 2021 at 17:02 UTC
    I understand that there's no list datatype - that perhaps it is used shorthand for something which can be evaluated in list context, but how can I reconcile this with the fact that subroutines return lists? Is it more correct to say that subroutines return a sequence of comma operators?
      I have to disagree with brother Choroba, Lists exist...

      ... but as temporary stack entries and not as allocated variables.

      So something like %h = @a (theoretically ) means:

      • push all array elements on a stack
      • read all stack elements into a hash
      (that's simplified, because optimization might kick in)

      There is also dedicated syntax for lists, like

      • () for empty list
      • (...)[n] for nth list element
      • , as list constructor (but only in list context)

      What you actually want is a list of pairs which doesn't exist in Perl, but in some other languages, like Raku afaik.

      Otherwise it's totally ambiguous to tell what keys LIST is supposed to mean, if LIST was a @array.

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2022-05-18 09:45 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (70 votes). Check out past polls.