I am looking for a function which can get me all the keys from hash or I can loop through the hash to retrieve single key at a time.
Currently I am hardcoding key
VALUE option = rb_hash_aref(options, rb_str_new2("some_key"));
You can iterate over the key/value pairs with a callback function using rb_hash_foreach (blog post w/an example):
void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE);
There is an rb_hash_keys in MRI, but it's not in any header files it seems, so using it may be risky.
You could always make a call to the Ruby method itself:
VALUE keys = rb_funcall(hash, rb_intern("keys"), 0)
Related
In Java, the equals/hash functions can be customised simply by overriding/implementing methods on a class.
This is very useful when you want to customise uniqueness of your class - so that you can check for 'duplicates' in a set easily.
How would you do the same thing in Elixir, specifically with ETS?
One way to do what I need to do is by making a unique hash function (that can return any type). There should only be one unique output of this hash function per unique input.
Then you can store the {hash, val} tuples:
table = :ets.create(:table, [])
:ets.insert(table, {hash(val), val})
:ets.lookup(table, hash(val))
In Java's hashmap:
map.put(key, new_value)
will update the entry of key=key with new_value if it exists in the hashmap.
What's the correct way to do the similar thing in unordered_map of C++11?
I haven't found an API like updateXXX, and the documentation says the unordered_map::insert function will succeed only when there isn't any such pair with a key.
If you know that the key is in the map, you can utilize operator[] which returns a reference to the mapped value. Hence it will be map[key] = new_value. Be careful, however, as this will insert a (key, new_value) if the key does not already exist in the map.
You can also use find which returns an iterator to the value:
auto it = map.find(key)
if(it != map.end())
it->second = new_value;
If the type value has no default constructor you can use:
map.emplace(key, new_value).first->second = new_value;
This also has all the other advantages of emplace vs [] operator and insert.
I thought that Java's map.put inserted the element if it wasn't already in the map and updated it if it was in the map, see put:
put
public V put(K key, V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
This would be equivalent to unordered_map::operator[]:
If k matches the key of an element in the container, the function returns a reference to its mapped value.
If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).
Check if the key exists
Update the value by referencing the key
if(map.count(key)){
map[key] = value;
}
I have a method drive that goes like this:
public double drive(double milesTraveled, double gasUsed)
{
gasInTank -= gasUsed;
return totalMiles += milesTraveled;
}
I know I can't return multiple values from a method, but that's kind of what I need to do because I need both of these values in my main method, and as it is now it's obviously only returning the one. I can't think of anything that would work. Sorry if this is a super beginner question. What can I do to get both values to return from the method?
You can return multiple value from a function. To do this You can use structure.
In the structure you can keep required field and can return structure variable after operation.
You can also make a class for the required field if You are using OOPS supporting language but Structure is best way.
In most languages you can only return a single value from a method. That single value could be a complex type, such as a struct, array or object.
Some languages also allow you to define output parameters or pass in pointers or references to outside storage locations. These kinds of parameters also allow you to return additional values from your method.
not sure, but can you take array of your values?
array[0]=gasInTank;
array[0] -= gasUsed;
array[1]=milesTraveled;
array[1] -= milesTraveled;
return array;
Is it possible in Puppet to change hashes keys without calling a custom functions within a class or custom type?
Pretty much calling regular ruby functions.
$myHash.keys.each { |k| $myHash[k + "_toto"] = $myHash[k]; $myHash.delete(k) }
I am using hashtable that contains around 60 key,value pairs.
for assigning the value based on key to any control in my page i have to explicitly type the keyname.
For example:
txtName.Text = htData["Name"].ToString();
txtAddress.Text = htData["Address"].ToString();
Doing this same thing for 60 values is time taking and inefficient.
is there a way to directly get the key and then set the value.
Just like intellisense in vs.
For example:
txtName.Text = htData.Name.Value.ToString();
txtAddress.Text = htData.Address.Value.ToString();
If you are stuck with the Hashtable there is not much you can do. I would suggest switching to the Dictionary class.
You could then make the Key an Enumeration:
public enum DataType
{
Name,
Adress,
...
}
Or you could make an Object with 60 Properties, without using a Dictionary.
If you are stuck with the Hashtable, you could still use an Enumeration, with ToString().
A Hashtable will never provide "nice" code though.