This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pass an associative array as argument to a function in Bash?
I declare my hash array:
declare -A some_array
And I declare my function:
some_function() {
..
}
How can I send the array as an argument to the function in order to access it?
I know that I can use it as a global variable, but it's not the way out when I have a lot of hash arrays I want to use with some function.
If there is no way to do it, how can I assign to the one hash array value of other?
Access it as a global variable (simply refer to it by name inside your function). There is no array passing in Bash. There are awkward techniques that try to do this, but I recommend avoiding the mess.
Other options include writing your entire script in a language such as Python or Perl which supports passing arrays, hashes or their references.
In Bash 4.3 or later you can use name references, but there are caveats.
Related
This question already has answers here:
What does map(&:name) mean in Ruby?
(17 answers)
Closed 4 years ago.
I've seen an example of how to sort a string. To sort case insensitively:
str.chars.sort(&:casecmp).join
#=> "ginrSt"
I'm curious about (&:casecmp). I found that for example:
arr.map(&:name)
is shorthand for
arr.map(&:name.to_proc)
which is same with
arr.map{|el| el.name}
I know the & (ampersand) tries to convert symbol to proc, and pass it as a block to a method. I do not understand how this would work for sort method, which is supposed to compare two values. Would it be as follows?
str.chars.sort{|a, b| a.casecmp ;b.casecmp}.join
It wouldn't be helpful since soft needs a block to return an integer and casecmp needs an argument. (Or is it called parameter in that case?) To me, it looks more like this:
str.chars.sort{|a, b| a.casecmp(b)}.join
How does &:casecmp know to take one of |a, b| as a caller and the other one as an argument? I wouldn't guess it that it is an option.
If more than one parameter is passed to your block, the proc created by Symbol#to_proc uses the additional block parameters as parameters to the method call.
http://phrogz.net/symbol-to-proc-with-multiple-arguments
So, what's really happening is, sort(&:casecmp) is converted to:
sort {|a,b| a.casecmp(b) }
because sort takes two parameters.
This question already has answers here:
How can I pass a slice as a variadic input?
(3 answers)
Closed 4 years ago.
I need to initialize gocql with multiple ips, I want to pass the ips from a variable/constant.
How to pass some thing like
gocql.NewCluster(ipvalues)
instead of using
gocql.NewCluster("127.0.0.1", "127.0.0.2")
i want to pass the list of ips through a variable something like an array.
As you can see, gocql.NewCluser takes a variadic parameter, which means you can pass multiple values separated with commas to the function.
In go, you just need to make your ipvalues variable be a slice of strings and pass it like this:
ipvalues := []string{"127.0.0.1", "127.0.0.2"}
gocql.NewCluster(ipvalues...)
This will have the same effect as writing gocql.NewCluster("127.0.0.1", "127.0.0.2")
See the golang spec for more information on this feature
This question already has an answer here:
Does Swift have something like "ref" keyword that forces parameter to be passed by reference?
(1 answer)
Closed 8 years ago.
Swift has the inout keyword to pass a primitive argument by reference. When would I use this over just passing it by value?
Edit: I realize that you can use this to change its value, but why not just pass it by value and assign it the corresponding value in the tuple returned by the function?
You would do that if you wanted to modify the original value instead of just a copy. However, I would argue that you should just return the new value since you can return multiple values in Swift.
This seems to be a plausible reason:
"Maybe the existing body of Objective C libraries have a lot of out parameters, and they didn't want to wrap them all for Swift."
http://blog.lexspoon.org/2014/06/my-analysis-of-swift-language.html
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Understanding ruby splat in ranges and arrays
could anyone tell me what the * does in the following piece of code?
line = "name=yabbi;language=ruby;"
Hash[*line.split(/=|;/)]
Thanks.
* is the splat operator. It is used to split an array into a list of arguments.
line.split(/=|;/) returns an array. To create a Hash, each element of the array must be passed as an individual parameter.
it's a splat operator Read about it. Often times you see it used when you want to split up an array to use as parameters of a function.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In Ruby, can you perform string interpolation on data read from a file?
I need to loop through a few 'li' elements:
for i in 1..5
Xpaths.getPath("location","elements")
end
Then, all xpaths are in an external file, so the value of such 'elements' variable is as follows:
{
"location":
{
"elements" :"//ul[#id='locations']/li[#{i}]/a"
}
}
The elements variable is read as a string, and [#{i}] is not replaced with values such as [1]. Is there a way to define a specific part of a string in an external file as a variable?
If I understand your question correctly, you have some external data file in JSON format, whose structure has certain fields as XPath strings on which you would like to perform Ruby string interpolation.
The short answer is that yes, you can do this directly using, say Kernel#eval. There are also other options such as using erb. A solution using the simple "eval" route might look like this:
xpaths = JSON.load(File.read('my-xpaths.json'))
(1..5).each do |i|
xpath = eval(xpaths['location']['elements'].to_json)
# => "//ul[#id='locations']/li[1]/a"
end
Of course, using "eval" is fraught with peril since you are essentially executing code from another source, so there are many precautions you must take to ensure safety. A better approach might involve doing a simple regular expression replacement so that you can constrain the interpolation on the XPath item:
xpaths['location']['elements'] # => "//ul[#id='locations']/li[__INDEX__]/a"
xpath = xpaths['location']['elements'].gsub(/__INDEX__/, i.to_s)
# => "//ul[#id='locations']/li[1]/a"
See also this related question: In Ruby, can you perform string interpolation on data read from a file?