Is there any advantage to using @array keys instead of 0 .. $ # array?

I was surprised to find that the keys function happily works with arrays:

 keys HASH keys ARRAY keys EXPR 

Returns a list of all keys in a named hash, or array indexes. (In a scalar context, returns the number of keys or indexes.)

Is there any use to using keys @array instead of 0 .. $#array in terms of memory usage, speed, etc., or reasons why this functionality is more of a historical origin?

When I see that keys @array supports the modification of $[ , I assume that it is historical:

 $ perl -Mstrict -wE 'local $[=4; my @array="a".."z"; say join ",", keys @array;' Use of assignment to $[ is deprecated at -e line 1. 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 
+6
source share
4 answers

There is one important reason in the link above why you can use / not use keys :

As a side effect, the calling keys () reset the internal HASH or ARRAY interpreter (see each). In particular, calling keys () in a void context resets the iterator without any additional overhead.

This will cause each to reset the beginning of the array. Using keys and each with arrays can be important if they ever initially support sparse arrays as a real data type.

All that has been said, with so many array-oriented language constructs like foreach and join in perl, I don’t remember the last time I used 0..$#array .

+3
source

Mark seems to be partly right. He lacks the fact that each of them now works with an array, and, like each of the hashes, each with arrays returns two elements for each call. Where each % hash returns the key and value, each @array also returns the key (index) and value.

 while (my ($idx, $val) = each @array) { if ($idx > 0 && $array[$idx-1] eq $val) { print "Duplicate indexes: ", $idx-1, "/", $idx, "\n"; } } 

Thanks to Zaid for the question, and jmcnamara for picking it up on CB CB perlmonks. I have not seen this before - I often sorted through the array and wanted to find out which index I was in. This waaaay is better than manually manipulating some $i variable created outside the loop and increasing inside, since I expect continue , redo , etc. It will survive better.

So, since we can now use each for arrays, we must be able to reset the iterator, and therefore have keys .

+4
source

I really think you answered your question: it returns the actual indexes of the array, regardless of what value you set for $[ . Thus, in terms of generality (especially for using the library), this is more preferable.

The version of Perl I (5.10.1) does not support the use of keys with arrays, so this cannot be for historical reasons.

+2
source

Well in your example, you put them on a list; So in the context of the list

keys @array will be replaced with all elements of the array while 0 .. $#array will do the same as slicing the array; So, instead of $array[0 .. $#array] you can also mention $array[0 .. (some specific index)]

0
source

Source: https://habr.com/ru/post/894544/


All Articles