Finding values from array in hash key and return counts of zero

tavalendo

I have one array which I am checking for values in a hash (which I created). The idea is to check each element of the array if exists in the hash keys. If it does not exists, it should say that the non-existent element appears zero times. The hash I created to count frequency of numbers on a second array.

Here's what I did:

use strict;
use warnings;

sub main {
    
    my @Source = ("A", "B", "D");
    my @ArrToHash = ("A", "C", "A", "B", "E", "F");
    
    my %hashy;
    ++$hashy{$_} for @ArrToHash;
    
    foreach my $e (@Source) {
        if (exists $hashy{$e}) {
            print "$e are $hashy{$e}\n";
        }
    }
}
main();

I am getting

A are 2

B are 1

While in reality I would like:

A are 2

B are 1

D are 0

Any clue? How to fix my code?

AnFi
    if (exists $hashy{$e}) {
        print "$e are $hashy{$e}\n";
    }else{
        print "$e are 0\n";
    }

OR

    printf "%s are %d\n", $e, $hashy{$e} // 0 ; 

Comment:
I prefer $hashy{$e} // 0. It returns $hashy{$e} if $hashy{$e} exits and it is not undef.
Ikegami prefers $hashy{$e} || 0. It returns 0 also when $hashy{$e} is an empty string. It works with older perl versions without // operator support.
IMHO It is a matter of "style-guide" in most situations.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

AutoMapper - how to return values from foreign key?

分類Dev

Randomly select key from array to return data mapped to that key of the array

分類Dev

Return specific values from array of hashes - JSON

分類Dev

MongoDB query to return multiple values from an array

分類Dev

Finding counts of unique values in column for each unique value in other column

分類Dev

How to add specific values from array list to the Hash map?

分類Dev

Is there a ruby or rails method that will return the hash key when checking for the hash key?

分類Dev

Using an array as a hash key in Ruby

分類Dev

Finding the indexed location of values in a unsorted numpy array from data in another unsorted numpy array

分類Dev

Count duplicate values in hash of an array

分類Dev

add a key value pair to hash in first array depending on the substring from second array

分類Dev

Return sorted unique values from array, but if count is equal, return values in order

分類Dev

Fastest way of finding and multiplying repeated values in array

分類Dev

Finding (and recording) the maximum values of slices of a numpy array

分類Dev

visiting hash with keys from array

分類Dev

Set specific values to zero in numpy array

分類Dev

Finding the index or unique values from a dataframe column

分類Dev

Finding the missing values from a given range

分類Dev

Ruby - How to invert a Hash with an array values?

分類Dev

Finding zero crossing in python

分類Dev

Giving int values to key in an array

分類Dev

replace key with new array values

分類Dev

How to return same values of the array

分類Dev

PHP - Get all values with specific array key from multidimensional array with unknown depth

分類Dev

Display values from one associative array whose key exists as the value in another array

分類Dev

How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

分類Dev

Creating a new array of objects by finding values in JSON data

分類Dev

Finding two values existing in same object array using javascript

分類Dev

Finding X values in numpy array and substituting for random value

Related 関連記事

  1. 1

    AutoMapper - how to return values from foreign key?

  2. 2

    Randomly select key from array to return data mapped to that key of the array

  3. 3

    Return specific values from array of hashes - JSON

  4. 4

    MongoDB query to return multiple values from an array

  5. 5

    Finding counts of unique values in column for each unique value in other column

  6. 6

    How to add specific values from array list to the Hash map?

  7. 7

    Is there a ruby or rails method that will return the hash key when checking for the hash key?

  8. 8

    Using an array as a hash key in Ruby

  9. 9

    Finding the indexed location of values in a unsorted numpy array from data in another unsorted numpy array

  10. 10

    Count duplicate values in hash of an array

  11. 11

    add a key value pair to hash in first array depending on the substring from second array

  12. 12

    Return sorted unique values from array, but if count is equal, return values in order

  13. 13

    Fastest way of finding and multiplying repeated values in array

  14. 14

    Finding (and recording) the maximum values of slices of a numpy array

  15. 15

    visiting hash with keys from array

  16. 16

    Set specific values to zero in numpy array

  17. 17

    Finding the index or unique values from a dataframe column

  18. 18

    Finding the missing values from a given range

  19. 19

    Ruby - How to invert a Hash with an array values?

  20. 20

    Finding zero crossing in python

  21. 21

    Giving int values to key in an array

  22. 22

    replace key with new array values

  23. 23

    How to return same values of the array

  24. 24

    PHP - Get all values with specific array key from multidimensional array with unknown depth

  25. 25

    Display values from one associative array whose key exists as the value in another array

  26. 26

    How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

  27. 27

    Creating a new array of objects by finding values in JSON data

  28. 28

    Finding two values existing in same object array using javascript

  29. 29

    Finding X values in numpy array and substituting for random value

ホットタグ

アーカイブ