recentpopularlog in

kme : hashes   11

md5sum on Mac OS X | check MD5 hashes
tl;dr: use 'md5', but it doesn't have a '-c' option.
macos  checksum  hashes  md5  sysadmin  security  sortof  solution 
12 weeks ago by kme
transform hash key from string into symbol - Ruby Forum |
This was handy, but I found a different way by just iterating over the array and returning another (two-element) array inside a 'map' or a 'each_key' or something, then passing this to the Hash#[] constructor.

<code class="language-ruby">class Hash
def symbolize_keys
replace(inject({}) { |h,(k,v)| h[k.to_sym] = v; h })
ruby  arrays  hashes  map  fp  idiom  newbie  syntax  solution 
may 2018 by kme
syntax - What do the different brackets in Ruby mean? - Stack Overflow |
[] can be overridden as a custom method, and is generally used to fetch things from hashes (the standard library sets up [] as a method on hashes which is the same as fetch)
There is also a convention that it is used as a class method in the same way you might use a static Create method in C# or Java. e.g.
<code class="language-ruby">
a = {1 => 2} # create a hash for example
puts a[1] # same as a.fetch(1), will print 2

Hash[1,2,3,4] # this is a custom class method which creates a new hash</code>
ruby  syntax  brackets  braces  idiom  arrays  hashes  annoyance  solution 
may 2018 by kme
how do i sort hash by key numerically []
#!/usr/bin/perl -w
%hash=( 89=>3, 45=>2, 1 =>5, 40=>3);
foreach (sort { $a <=> $b } keys(%hash) )
print "key: $_ value: $hash{$_}\n"
perl  sorting  arrays  hashes  solution 
november 2016 by kme
dictionary - How can I combine hashes in Perl? - Stack Overflow
%newHash = (%hash1, %hash2); %hash1 = %newHash;

## or else ...

@hash1{keys %hash2} = values %hash2;
perl  hashes  datastructure  maybesolution 
december 2015 by kme
Merge hash into another hash
%target{keys %$source} = values %$source if defined $source && ref $source eq 'HASH';
perl  hashes  datastructures  maybesolution 
december 2015 by kme

Copy this bookmark:

to read