0

The title say it all. I am trying to find good naming convention for my functions. Some suggestions are here but I would like to know your opinion and if it is possible to use shorter name and still preserve the meaning of the function. This is my function:

sub convert_AoA_to_HoA_where_hash_key_is_element_from_particular_inner_array_index_and_hash_value_is_reference_to_this_inner_array(){
  my ($argv1, $argv2, $argv3) = @_;
  my @AoA = @$argv1;
  my $HoA = $argv2;
  my $index = $argv3;

    foreach my $array (@AoA){
    $HoA->{"$array->[$index]"} = $array;
 }
}
Community
  • 1
  • 1
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
  • Is your function name coming out in movie form soon? I tried to read the novelization, but I couldn't make it. Try and use something shorter. – Elliott Frisch Jun 24 '14 at 14:12
  • 6
    I like the combination of incredibly verbose function name with entirely generic parameter names. – RobEarl Jun 24 '14 at 14:14
  • 1
    Subroutine name like that is absurd. Keep the name as short as possible but descriptive. Such as sub AoA2HoA {....}. The detailed description of what it does could be a comment inside and at the beginning of the sub. – Ron Bergin Jun 24 '14 at 14:23
  • Your current name for it could just be a comment above it. – hmatt1 Jun 24 '14 at 15:05
  • `Perl Best Practices' by Damian Conway can help you there – Pierre Jun 24 '14 at 15:05
  • `sub create_keyed_recordset { my ($key_index, $recordset) = @_; return map { $_->[$index] => $_ } @$recordset }` – ikegami Jun 24 '14 at 15:22

2 Answers2

2

If you are having to do this, this may be indicating something about the suitability of the existing data structures to begin with.

As mpapec demonstrates, the functionality is simple enough to replicate without the need for a dedicated subroutine using map.

Since the keys of the resultant hash are the values of a particular column, an alternative could be to use a hash slice to clarify intent:

my %hash;
@hash{ map $_->[$index], @AoA } = @AoA;

The only time when I would consider having to roll this into a subroutine would be if I had no control over the incoming data structure and needed to output separate hashes for different indices. Having said that, rehash_by_column_index is a more-befitting name for such a sub.

Community
  • 1
  • 1
Zaid
  • 36,680
  • 16
  • 86
  • 155
2

Without going into philosophical debate, you don't need to make it a function,

my %HoA = map { $_->[$index] => $_ } @AoA;
mpapec
  • 50,217
  • 8
  • 67
  • 127