Skip to content

Instantly share code, notes, and snippets.

@smujohnson
Created August 23, 2012 17:03
Show Gist options
  • Save smujohnson/3438752 to your computer and use it in GitHub Desktop.
Save smujohnson/3438752 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
# Notice that the values are all equal for some of the keys
my @stuff = (
{ a => "1", b => "2", c => '4'},
{ a => "1", b => "3", c => '4'},
{ a => "1", b => "2", c => '4'},
{ a => "1", b => "2", c => '4'},
{ a => "1", b => "2", c => '4', d => '2'},
);
my %hash_count;
foreach my $ref_row (@stuff) {
foreach my $key_letter (keys %$ref_row) {
$hash_count{$key_letter}{ $ref_row->{$key_letter} }++;
}
}
my @keys_total = keys %hash_count;
say "All the keys in \@stuff: @keys_total";
my @keys_equal;
foreach my $key_letter (keys %hash_count) {
if (keys %{ $hash_count{$key_letter} } == 1) {
push @keys_equal, $key_letter;
}
}
say "The keys for which all values are equal: @keys_equal";
@Osse
Copy link

Osse commented Aug 23, 2012

This looks

std::cout << "very good!" << std::endl;

Indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment