hash

How to test order-conscious equality of hashes

大憨熊 提交于 2019-12-23 12:04:56
问题 Ruby 1.9.2 introduced order into hashes. How can I test two hashes for equality considering the order? Given: h1 = {"a"=>1, "b"=>2, "c"=>3} h2 = {"a"=>1, "c"=>3, "b"=>2} I want a comparison operator that returns false for h1 and h2 . Neither of the followings work: h1 == h2 # => true h1.eql? h2 # => true 回答1: Probably the easiest is to compare the corresponding arrays. h1.to_a == h2.to_a 回答2: You could compare the output of their keys methods: h1 = {one: 1, two: 2, three: 3} # => {:one=>1,

How can I use a Perl hash key that has a literal dot?

自闭症网瘾萝莉.ら 提交于 2019-12-23 10:54:32
问题 I have a hash in Perl which has been dumped into from some legacy code the name of the key has now changed from simply reqHdrs to reqHdrs.bla $rec->{reqHdrs.bla} My problem is now I cant seem to access this field from the hash any ideas? The following is my error Download Script Output: Bareword "reqHdrs" not allowed while "strict subs" in use 回答1: As described in perldoc perldata: ...An identifier within such curlies is forced to be a string, as is any simple identifier within a hash

Why can't I make a new hash from selected keys in ruby?

回眸只為那壹抹淺笑 提交于 2019-12-23 10:53:37
问题 This has been bugging me for a while. It's not a difficult thing, but I don't know why there's no easy way to do it already, and I bet there is and I don't see it. I just want to take a hash, like this: cars = {:bob => 'Pontiac', :fred => 'Chrysler', :lisa => 'Cadillac', :mary => 'Jaguar'} and do something like cars[:bob, :lisa] and get {:bob => 'Pontiac', :lisa => 'Cadillac'} I did this, which works fine: class Hash def pick(*keys) Hash[select { |k, v| keys.include?(k) }] end end ruby-1.8.7

Java: Hash function for objects

痴心易碎 提交于 2019-12-23 10:09:30
问题 I'm thinking about a hash function for arbitrary Java-objects for exercise means. The naive way would be to call the hashCode()-function for each attribute, add these hashes and then take the sum modulo the maximal hash value, or something like that. However, that would mean, that the hash value would change whenever on of the attributes is changed, so this method cannot be used if you want to store objects in a hash table. The hash code of an object should represent its identity. But how can

How can I merge several hashes into one hash in Perl?

断了今生、忘了曾经 提交于 2019-12-23 10:08:06
问题 In Perl, how do I get this: $VAR1 = { '999' => { '998' => [ '908', '906', '0', '998', '907' ] } }; $VAR1 = { '999' => { '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] } }; $VAR1 = { '999' => { '996' => [] } }; $VAR1 = { '999' => { '995' => [] } }; $VAR1 = { '999' => { '994' => [] } }; $VAR1 = { '999' => { '993' => [] } }; $VAR1 = { '999' => { '997' => [ '986', '987', '990', '984', '989', '988' ] } }; $VAR1 = { '995' => { '101' => [] } }; $VAR1 = { '995' => {

Perl Module Error - defined(%hash) is deprecated

可紊 提交于 2019-12-23 10:00:02
问题 Background: I am working to migrate a Linux server to a newer one from Ubuntu 10.04 to 12.04 This server is responsible for executing several a number of Perl modules via crontabs. These Perl Modules rely heavily on 30-40 perl extensions. I have installed all Perl extensions and the crontabs are able to process successfully except for several Syntax errors caused by the newer versions of these Perl extensions. I need some help with modifying the syntax to get the Perl script to process as

Generate ID from string in Python

爷,独闯天下 提交于 2019-12-23 09:57:12
问题 I'm struggling a bit to generate ID of type integer for given string in Python. I thought the built-it hash function is perfect but it appears that the IDs are too long sometimes. It's a problem since I'm limited to 64bits as maximum length. My code so far: hash(s) % 10000000000 . The input string(s) which I can expect will be in range of 12-512 chars long. Requirements are: integers only generated from provided string ideally up to 10-12 chars long (I'll have ~5 million items only) low

How to convert C# hashed byte array to string for passing to API?

可紊 提交于 2019-12-23 09:56:57
问题 I have a number of values that must be combined into a SHA256 hash to be passed to a web service. These values are combined into a byte array using Encoding.ASCII.GetBytes(allparametershere) and then hashed to SHA256 by myHashMethod.ComputeHash(allParameterByteArray). Since I have to add this value to a request header, it must be passed as a string to the request header. The requirements of the 3rd party system state that it must be in 64 character Hex format of the string. I've used Convert

fetch resource, compute hash, return promise

折月煮酒 提交于 2019-12-23 09:56:46
问题 I would like to use the Fetch API in a browser extension to download a resource and compute a hash thereof. The following works (using crypto through Browserify) fetch(url).then(function(response) { return response.blob(); }).then(function(data) { var a = new FileReader(); a.readAsBinaryString(data); a.onloadend = function() { var hash = crypto.createHash(hashType); hash.update(a.result, 'binary'); return hash.digest('hex'); }; }) but has the disadvantage that I have to wait for a.onloadend

Hashing an Image in Javascript

江枫思渺然 提交于 2019-12-23 09:48:51
问题 I have currently been trying to hash an image from my browser using javascript. However, I've been hashing a string of the dataURL or the pixel data that I've been retrieving from the canvas element in HTML. This is obviously not the same as hashing the raw data of the image which is what I would like to do. For example the data that would be used for the same image in the php hash file function. Does anybody know how I can access this raw image data using javascript to get a hash value that