Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^6: Using 'keys' on a list (update x2)

by Anonymous Monk
on Jun 30, 2021 at 15:35 UTC ( [id://11134500]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Using 'keys' on a list (update x2)
in thread Using 'keys' on a list

that's the same as scalar(%hash) not scalar(list), try again
  • Comment on Re^6: Using 'keys' on a list (update x2)

Replies are listed 'Best First'.
Re^7: Using 'keys' on a list (update x2)
by The Perlman (Scribe) on Jun 30, 2021 at 16:14 UTC
    From the OP:

    "Suppose a function f() returns an even-sized list..."

    - Ron
      From the OP: "Suppose a function f() returns an even-sized list..."

      so what? sub f { %hash } scalar(f) is the same as scalar(%hash)

        modified: my fault, I do this on one line, and I rewrote code when I post them, I trust they are same.
        apparently, they are not.

        and I find I should just quote this:"Called in list context, ...", from perldoc...

        Only focus on op's condition...

        use strict; use warnings; use feature 'say'; sub f {a => 1, b=>2} my $foo = f(); my $bar = join '_', f(); say $foo; say $bar; #outputs 2 a_1_b_2

        so that's why I assume the first argument keys accept is 'a', like join.
        I just want to explain keys don't work for that single scalar value.

        but but... keys looks like do not accept arguments in scalar context
        if we write:

        sub f {a => 1, b=>2} my @all_key = keys f();
        the warning message is "Type of arg 1 to keys must be hash or array (not subroutine entry)"
        so I tried it:
        sub f {a => 1, b=>2} my %h = (c => 3); say keys %h, f(); #output: c
        and then if we write:
        sub f {a => 1, b=>2} my %h = (c => 3); say keys f(), %h; #warnings: Experimental keys on scalar is now forbidden at perl.pl line 11. Type of arg 1 to keys must be hash or array (not subroutine entry) at +perl.pl line 11, near ")," Execution of perl.pl aborted due to compilation errors.
        so, this is why I think it is 'a'.
        I guess the first argument of keys is the first element in list.
        p.s. I should come back earlier...


        edit: my fault, I type '}' as ')' ... thanks The Perlman and LanX, I had modified them:(
        yes, I find every result different now. I write them on one line... here I redo all:

        use strict; use warnings; use feature 'say'; sub f {a => 1, b=>2} my $foo = f(); my $bar = join '_', f(); say $foo; say $bar; #output 2 a_1_b_2 sub f {a => 1, b=>2} my @all_key = keys f(); #warnings: Experimental keys on scalar is now forbidden at call_art.pl line 7. Type of arg 1 to keys must be hash or array (not subroutine entry) at +call_art.pl line 7, near ");" Execution of call_art.pl aborted due to compilation errors. sub f {a => 1, b=>2} my %h = (c => 3); say keys %h, f(); #output ca1b2 sub f {a => 1, b=>2} my %h = (c => 3); say keys f(), %h; #warnings: Experimental keys on scalar is now forbidden at call_art.pl line 8. Type of arg 1 to keys must be hash or array (not subroutine entry) at +call_art.pl line 8, near ")," Execution of call_art.pl aborted due to compilation errors.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-23 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found