Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Type of arg 1 to splice must be array (not keys)

by jeanluca (Deacon)
on Dec 14, 2009 at 13:11 UTC ( [id://812705]=note: print w/replies, xml ) Need Help??


in reply to Re: Type of arg 1 to splice must be array (not keys)
in thread Type of arg 1 to splice must be array (not keys)

Thats a good solution. The only problem is that I don't know the number of keys. So I can have more or less. Which means I have to rewrite your solution to
my @k = (keys %mh)[0 .. (scalar keys(%my) <= 100 ? scalar keys(%mh) : +100)] ;
Which doesn't look that good anymore. So I prefer 'splice'. Is there a way to convert this 'key' list into an array ?

Replies are listed 'Best First'.
Re^3: Type of arg 1 to splice must be array (not keys)
by JavaFan (Canon) on Dec 14, 2009 at 13:15 UTC
Re^3: Type of arg 1 to splice must be array (not keys)
by ikegami (Patriarch) on Dec 14, 2009 at 15:36 UTC

    Your slice solution is buggy and uses scalar needlessly.

    my $keys = keys(%my) < 100 ? keys(%mh) : 100; my @k = ( keys %mh )[0..$keys-1];
    or
    my $keys = min 100, keys(%mh); my @k = ( keys %mh )[0..$keys-1];

    (List::Util provides min)

    A splice solution:

    my @k = keys %mh; splice( @k, 100 ) if @k > 100;

    You need the conditional to avoid the warning.

    Update: Added fix to OP's slice solution.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (9)
As of 2024-03-28 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found