hash

Convert array of hashes to array of structs?

Let's say I have two objects: User and Race. class User attr_accessor :first_name attr_accessor :last_name end class Race attr_accessor :course attr_accessor :start_time attr_accessor :end_time end Now let's say I create an array of hashes like this: user_races = races.map{ |race| {:user => race.user, :race => race} } ...

Is this the correct way to build a Perl hash that utilizes arrays?

This is the first time I have manipulated hashes and arrays in this way -- and it is working. Basically, for every key there are multiple values that I want to record and then print out in the form "key --> value --> value --> val..." My code is as follows. I am surprised that it works, so concerned that it works "by mistake". Is this t...

how .hash string method work ?

I'm just a newbie to ruby. I've seen a string method (String).hash . For example, in irb, I've tried >> "mgpyone".hash returns => 144611910 how does this method works ? ...

Passing hash as values in hidden_field_tag

I am trying to pass some filters in my params through a form like so: hidden_field_tag "filters", params[:filters] For some reason the params get changed in the next page. For example, if params[:filters] used to be... "filters"=>{"name_like_any"=>["apple"]} [1] ...it gets changed to... "filters"=>"{\"name_like_any\"=>[\"apple\"]}"...

How can I sort a Perl array of array of hashes?

@aoaoh; $aoaoh[0][0]{21} = 31; $aoaoh[0][0]{22} = 31; $aoaoh[0][0]{23} = 17; for $k (0 .. $#aoaoh) { for $i(0.. $#aoaoh) { for $val (keys %{$aoaoh[$i][$k]}) { print "$val=$aoaoh[$i][$k]{$val}\n"; } } } The output is: 22=31 21=31 23=17 but i expect it to be 21=31 22=31 ...

Short Python alphanumeric hash with minimal collisions

I'd like to set non-integer primary keys for a table using some kind of hash function. md5() seems to be kind of long (32-characters). What are some alternative hash functions that perhaps use every letter in the alphabet as well as integers that are perhaps shorter in string length and have low collision rates? Thanks! ...

Efficiently generate a 16-character, alphanumeric string

I'm looking for a very quick way to generate an alphanumeric unique id for a primary key in a table. Would something like this work? def genKey(): hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base64") alnum_hash = re.sub(r'[^a-zA-Z0-9]', "", hash) return alnum_hash[:16] What would be a good way to generate random nu...

Mapping multiple keys to the same value in a Javascript hash

I use a Javascript hash object to store a set of numerical counters, set up like this [this is greatly simplified]: var myHash = { A: 0, B: 0, C: 0 }; Is there a way to make other keys, ones not explicitly in myHash, map to keys that are? For instance, I'd like [again, this is simplified]: myHash['A_prime']++; // or myHas...

How can I take a reference to specific hash value in Perl?

How do I create a reference to the value in a specific hash key. I tried the following but $$foo is empty. Any help is much appreciated. $hash->{1} = "one"; $hash->{2} = "two"; $hash->{3} = "three"; $foo = \${$hash->{1}}; $hash->{1} = "ONE"; #I want "MONEY: ONE"; print "MONEY: $$foo\n"; ...

How can I add only unique values to an anonymous array used as a hash value?

EDIT Sorry I forgot the most important part here. Each key can have more than one value. Apologies to those who already answered. print and join will be used later to print multiple values for $key on a single line. In the example code below, assuming that the value $keyvalue is constantly changing, I am attempting to use a single line ...

Is it possible to create a file with a given size and MD5 hash?

Of course I know is possible to create a file with a certain size and how to do it. But is it possible to create a file with a certain size and also a certain MD5 fingerprint? ...

Generate unique hashes for django models

I want to use unique hashes for each model rather than ids. I implemented the following function to use it across the board easily. import random,hashlib from base64 import urlsafe_b64encode def set_unique_random_value(model_object,field_name='hash_uuid',length=5,use_sha=True,urlencode=False): while 1: uuid_number = str(ra...

PHP - Generate an 8 character hash from an integer

Is there a way to take any number, from say, 1 to 40000 and generate an 8 character hash? I was thinking of using base_convert but couldn't figure out a way to force it to be an 8 character hash. Any help would be appreciated! Thanks. ...

File Comparison Strategies

I'm searching for strategies one might use to programmatically find files which may be duplicates of each other. Specifically in this case, videos. I'm not looking for exact matches (as nice as that would be in the land of rainbows and sunshine). I'm just looking to collect pairs of video which content might be the same so that a huma...

Merge a hash with the key/values of a string in ruby

Hi there, I'm trying to merge a hash with the key/values of string in ruby. i.e. h = {:day => 4, :month => 8, :year => 2010} s = "/my/crazy/url/:day/:month/:year" puts s.interpolate(h) All I've found is to iterate the keys and replace the values. But I'm not sure if there's a better way doing this? :) class String  def interpolate...

Hashing the state of a complex object in .NET

Some background information: I am working on a C#/WPF application, which basically is about creating, editing, saving and loading some data model. The data model contains of a hierarchy of various objects. There is a "root" object of class A, which has a list of objects of class B, which each has a list of objects of class C, etc. Ar...

How can I pass a hash to a Perl subroutine?

In one of my main( or primary) routines,I have two or more hashes. I want the subroutine foo() to recieve these possibly-multiple hashes as distinct hashes. Right now I have no preference if they go by value, or as references. I am struggling with this for the last many hours and would appreciate help, so that I dont have to leave perl ...

mootools hash() in jquery?

Hi, I have been using mootools for a year now. I need to use jquery for my new projects. I always used hash() to make namespaces for my functions in mootools. For example, var person = new Hash({ say_name: function(){ }, say_age: function(){ } }); Does Jquery has similar stuff? ...

Python: How do sets work

I have a list of objects which I want to turn into a set. My objects contain a few fields that some of which are o.id and o.area. I want two objects to be equal if these two fields are the same. ie: o1==o2 if and only if o1.area==o2.area and o1.id==o2.id. I tried over-writing __eq__ and __cmp__ but I get the error: TypeError: unhashable...

Program to change/obfuscate all hashes (MD5/SHA1) in a directory tree?

Hi fellas, A) Are there any FOSS programs out there that can manage to hashchange all files in a directory tree? B) Failing that, what methods could be used to develop this capability in a (crappy) self-written program without requiring the program to be sophisticated and content-aware? C) [Answered] Is there any (roughly) univers...