function to get a random element of an array bash [duplicate] - arrays

This question already has answers here:
How to pass array as an argument to a function in Bash
(8 answers)
Closed 8 years ago.
I know how to get a random item from an array like that:
declare -a ARRAYISO=(100 200 400 800)
echo ${ARRAYISO["$[RANDOM % ${#ARRAYISO[#]}]"]}
I could obviously do that for each array, like a donkey, but I'd like to make a function which takes an array as argument and returns a random element.
I'm trying with:
randArrayElement() {
randElement=${$1["$[RANDOM % ${#$1[#]}]"]}
echo $randElement
}
randArrayElement ARRAYISO
but it doesn't like my $1... I've tryed with ", ', ` , bash doesn't interpret the $1 var...

Change your function to:
randArrayElement(){ arr=("${!1}"); echo ${arr["$[RANDOM % ${#arr[#]}]"]}; }
and call it as:
randArrayElement "ARRAYISO[#]"

Related

Powershell - declaring array of arrays is not so easy? [duplicate]

This question already has answers here:
PowerShell enumerate an array that contains only one inner array
(2 answers)
Closed 2 years ago.
I tried to describe a small 'list' of things, using arrays of arrays. An odd behaviour I observed:
function write-it-out([array] $arrays)
{
foreach($a in $arrays)
{
write-host "items" $a[0] $a[1]
}
}
$arrayOfArrays1 = #(
#("apple","orange"),
#("monkey","bear")
)
$arrayOfArrays2 = #(
#("android","linux")
)
# it works
write-it-out $arrayOfArrays1
# it wont
write-it-out $arrayOfArrays2
The first case outputs the expected two lines with the following content:
items apple orange
items monkey bear
But the second function call outputs not the expecteds
items android linux
but
items a n
items l i
Does somebody know why? And how to describe an array containing only one array inside, not more than one? So how to fix it? Thank guys in advance!
I'm not exactly sure why, but when you declare $arrayOfArrays2, PowerShell is immediately unrolling the outer array.
> $arrayOfArrays2.Count
2
> $arrayOfArrays2[0]
android
In order to make it not do that, you can add an extra comma inside the outer array declaration like this.
> $arrayOfArrays2 = #(,#("android","linux"))
> $arrayOfArrays2.Count
1
> $arrayOfArrays2[0]
android
linux
> write-it-out $arrayOfArrays2
Items android linux

Is there a way to declare associative arrays in bash using ip addresses as indices? [duplicate]

This question already has answers here:
why do I get an error 'invalid arithmetic operator'?
(2 answers)
"invalid arithmetic operator" in shell
(3 answers)
Closed 4 years ago.
I am trying to do this:
declare -a ip_array=( [127.0.0.1]=127.1.1.1 [127.1.1.1]=127.0.0.1 )
However bash complains as it thinks the decimals are operators:
bash: 127.0.0.1: syntax error: invalid arithmetic operator (error token is "127.0.0.1")
I tried using single and double quotes but it seems to evaluate arithmetically every time.
EDIT: I did not notice that there is a difference between lowercase -a and uppercase -A options when declaring an array.
[- +] a declares NAMEs as an indexed array (deleting with + acceptable syntax, but results in an error message)
[- +] A declares NAMEs as an associative array
Try this:
declare -A AR=( [127.0.0.1]=127.1.1.1 [127.1.1.1]=127.0.0.1 ); echo ${AR[#]};

How to set an Array that references variables, that also references variables [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Closed 4 years ago.
Random Variables
VARA=('blah')
VARB=('blah2')
VARC=('blah3' 'alt_blah3')
MASTERVAR=${VARA}${VARB}${VARC[0]}
ULTIMATEVAR=${VARA}${VARB}${VARC[1]}
What I want: An Array that references MASTERVAR and ULTIMATEVAR.
ULTIMATEMASTERVAR=(${MASTERVAR} ${ULTIMATEVAR})
What I've tried.
ULTIMATEMASTERVAR=('${MASTERVAR}' '${ULTIMATEVAR}')
ULTIMATEMASTERVAR=(${!MASTERVAR} ${!ULTIMATEVAR})
Wanted Result:
echo ${ULTIMATEMASTERVAR[0]}
Which prints out
blahblah2blah3
How to refer to other variables in an array:
$ foo=(abc)
$ bar=(def)
$ all=(foo bar)
$ echo "${all[0]}"
foo
$ echo "${!all[0]}"
abc
Loop through all to get the first element of each of them.

How to use a cell array directly returned from a function? [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 5 years ago.
I have a path (without a file name at the end) as a string in Matlab and i want to receive the first parent directory (the directory after the last file seperator character) in it.
At the moment i'm doing it like this:
>>filePath = 'D:\TRAIN_DATA\OBSTACLES\DOF';
>>firstParent = strsplit(filePath , filesep);
>>firstParent = firstParent{end};
>>disp(firstParent);
DOF
Is there any way i can use strsplit's return value (a cell array) without assinging it to a variable first?
Something like:
>>filePath = 'D:\TRAIN_DATA\OBSTACLES\DOF';
>>firstParent = ( strsplit(filePath , filesep) ){end};
>>disp(firstParent);
DOF
Do you mean:
[~,firstParent] = fileparts ( 'D:\TRAIN_DATA\OBSTACLES\DOF' )

how to generate a new array in perl each time a loop runs [duplicate]

This question already has answers here:
Creating arrays dynamically in Perl
(4 answers)
Closed 6 years ago.
I am running a loop and each time i need to store some information in a new array. How to generate a new array each time the loop runs like #array1, #array2 , #array3 and so on?
What you want is an array of arrays, i.e. a multi-dimensional array.
my #arrays;
for my $i(0..10)
{
$arrays[$i] = ['data1', 'data2'];
}
print $arrays[0][0];
print $arrays[0][1];

Resources