dirtdog has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I've got a working snippet of code that will take parameters and stuff them into a hash to be used later in the program to only keep the highest ranked value only (0 is top ranked) if it exists.

hierarchy.pl POPE PATRIARCH ARCHBISHOP CARDINAL
#!/usr/bin/env perl use strict; my @hierarchy; my %hierarchy; my $order =0; while ($#ARGV >= 0) { push @hierarchy,shift; } for (@hierarchy) { $hierarchy{$_} = $order; $order++; } while( my( $key, $value ) = each %hierarchy ){ print "$key: $value\n"; }

The code appears to work, but I thought there might be a better way to do it so thought I'd check with the Monks.

Much appreciated!

Replies are listed 'Best First'.
Re: HASH Hierarchy
by hippo (Archbishop) on Feb 16, 2018 at 15:49 UTC

    What you have there is fine. Although, if you don't need it for anything else, you can eliminate @hierarchy and shorten the code a bit. Don't forget warnings too.

    #!/usr/bin/env perl use strict; use warnings; my $order = 0; my %hierarchy; $hierarchy{$_} = $order++ for @ARGV; while( my( $key, $value ) = each %hierarchy ){ print "$key: $value\n"; }

      much more elegant...thank you!

Re: HASH Hierarchy
by Eily (Monsignor) on Feb 16, 2018 at 16:22 UTC

    Unless you can have gaps in your orders (eg: POPE at 0 and CARDINAL at 3 but nothing in between) you can consider your array as a hash that uses a number as key (you just access elements with $hierarchy[$pos] instead of $hierarchy{$pos}). BTW, if you are using the position to select your data rather than the name, it should be the key, not the value.

    When I say that an array can be used as a hash (with keys 0..N), you can even use each on it:

    while (my ($key, $value) = each @ARGV) { print "$value: $key\n"; # The key is the position }
    And if your version of perl is not too old, use a hash slice to extract pairs of position/element:
    use Data::Dumper; print Dumper { %ARGV[0,1,3] };

    Edit: so you can also get your hash with %hierarchy = reverse %ARGV[0..$#ARGV] if you want to keep the names as key.

Re: HASH Hierarchy
by thanos1983 (Parson) on Feb 16, 2018 at 15:57 UTC

    Hello dirtdog,

    You do not need to push into an array the @ARGV it is a already an array. Sample of code bellow:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $count = 0; my %hasHierarchy; $hasHierarchy{$_} = $count++ foreach @ARGV; while( my( $key, $value ) = each %hasHierarchy ){ print "$key: $value\n"; } # print Dumper \%hasHierarchy; __END__ $ perl test.pl POPE PATRIARCH ARCHBISHOP CARDINAL POPE: 0 CARDINAL: 3 ARCHBISHOP: 2 PATRIARCH: 1

    You can also use Data::Dumper to view complex structures. On the sample of code I have enter the command remove the # and give it a try.

    Update: Just a minor note to add, you can use also while loop to process the @ARGV, usually while loops are faster than foreach loops. Have a look on the Benchmark bellow, but also keep in mind that the while loop because we are using shift we will destroy the array. In case you want to reuse the array you need to copy it. In this case foreach loop is faster so a better option. :)

    Update 2: I tried to apply minor modifications but still foreach is winning, but at least now they are closer on the comparison. :)

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!