in reply to Re^8: Using 'keys' on a list (update x2)
in thread Using 'keys' on a list
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:
the warning message is "Type of arg 1 to keys must be hash or array (not subroutine entry)"sub f {a => 1, b=>2} my @all_key = keys f();
so I tried it:
and then if we write:sub f {a => 1, b=>2} my %h = (c => 3); say keys %h, f(); #output: c
so, this is why I think it is 'a'.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.
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^10: Using 'keys' on a list (prototype & backwards compatibility)
by LanX (Saint) on Jul 01, 2021 at 12:56 UTC | |
by glycine (Sexton) on Jul 01, 2021 at 13:41 UTC | |
Re^10: Using 'keys' on a list (update x2)
by LanX (Saint) on Jul 01, 2021 at 18:55 UTC | |
by AnomalousMonk (Archbishop) on Jul 01, 2021 at 21:44 UTC | |
by LanX (Saint) on Jul 01, 2021 at 21:51 UTC | |
Re^10: Using 'keys' on a list (update x2)
by The Perlman (Scribe) on Jul 01, 2021 at 10:16 UTC | |
by glycine (Sexton) on Jul 01, 2021 at 13:18 UTC |
In Section
Seekers of Perl Wisdom