Bash: Calculating in array and storing result in array
I have an array called arrayini which stores numbers. I want to take log to the base 2 of each of the numbers in that array and put it in file called result. I've used the following code to do it.
Code:
size=${#arrayini[@]} Code:
unset arrayresult |
You need to set it up as an embedded command.
Code:
arrayresult[$i]=$( echo "scale = 12; l(${arrayini[$i]})/l(2)" | bc -l ) Edit: Also, don't you need to echo the string into bc? By the way, you can output a list of an array's index numbers with ${!array[@]}. It can be convenient and more accurate when looping through all of an array's elements. Code:
for i in ${!arrayini[@]} Code:
printf "%s\n" ${arrayresult[@]} >FILE2 |
Quote:
|
Hi
I think what takes most of the time is calling bc 230000 times. Why not call it once instead? Also l(2) can be computed once instead of 230000 times. Code:
#!/bin/bash I'm slow, answers came while i was typing. |
Nevermind.
|
Quote:
|
Quote:
|
All times are GMT -5. The time now is 03:02 PM. |