Loop Through Array of Arrays of String with Spaces

How to iterate over an array of strings with spaces bash

You should quote your variables (using an unquoted variable should be the exception):

for func in "${dotfilesFunctionAlias[@]}"; do
echo "$func";
done;

Without double quotes, ${dotfilesFunctionAlias[@]} is expanded and func takes the value of each word in turn. Double quotes preserve the spaces.

Also, you should be cautious with echo and use printf instead. If the name of one of your functions starts with a valid echo option (-e, -n, etc), then echo "$func" will not work as intended.

Finally, with printf you don't need any loop at all. The following command is enough to print your array.

printf '%s\n' "${dotfilesFunctionAlias[@]}"

Loop through an array of strings in Bash?

You can use it like this:

## declare an array variable
declare -a arr=("element1" "element2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
echo "$i"
# or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also

Also works for multi-line array declaration

declare -a arr=("element1" 
"element2" "element3"
"element4"
)

Bash array with spaces in elements

I think the issue might be partly with how you're accessing the elements. If I do a simple for elem in $FILES, I experience the same issue as you. However, if I access the array through its indices, like so, it works if I add the elements either numerically or with escapes:

for ((i = 0; i < ${#FILES[@]}; i++))
do
echo "${FILES[$i]}"
done

Any of these declarations of $FILES should work:

FILES=(2011-09-04\ 21.43.02.jpg
2011-09-05\ 10.23.14.jpg
2011-09-09\ 12.31.16.jpg
2011-09-11\ 08.43.12.jpg)

or

FILES=("2011-09-04 21.43.02.jpg"
"2011-09-05 10.23.14.jpg"
"2011-09-09 12.31.16.jpg"
"2011-09-11 08.43.12.jpg")

or

FILES[0]="2011-09-04 21.43.02.jpg"
FILES[1]="2011-09-05 10.23.14.jpg"
FILES[2]="2011-09-09 12.31.16.jpg"
FILES[3]="2011-09-11 08.43.12.jpg"

Better way to convert string with multiple spaces into array

Use preg_split(), with 2 or more spaces as the delimiter.

$array = preg_split('/\s{2,}/', $line);

Reading space seperated string into array in while loop

In order to get each element of the array, you can loop through the variable own with a for loop:

stat -c %U file*.* | { read -ra own; for i in ${own[@]}; do echo $i; done; }

Another way of doing it is:

{ read -ra own; for i in ${own[@]}; do echo $i; done; } <<< $(stat -c %U file*.*)

Note that you don't need a while loop because you get everything on one line.

Array elements with spaces are arguments to a function

If you want word splitting to occur, then you shouldn't quote your variable:

myFunction $elem

Here's an example, using printf to demonstrate:

$ elements=("first" "second" "a third" "a fourth")
$ func() { printf '/tmp/file_%s_%s\n' "$1" "$2"; }
$ for elem in "${elements[@]}"; do func $elem; done
/tmp/file_first_
/tmp/file_second_
/tmp/file_a_third
/tmp/file_a_fourth

array.split to create a string with multiple whitespaces

Array.join is used to concatenate all the array's values, with the given string between them:

['foo','bar'].join(',') // = 'foo,bar'

You are expecting Array(2).join(' ') to be two spaces, but it will be only one since it's concatenating two empty values with a space in between.

Just remove the -1 at row 5 and it will work as expected:

function towerBuilder(nFloors) {    // build here    var towers = [];    var stars = "*";    var spaceNo = nFloors; // was nFloors - 1    let spaces = Array(spaceNo).join(" ");    spaceNo -= 1;    towers[0] = spaces + stars + spaces;    for(i = 1; i <= nFloors -1; i++)    {      stars = stars + Array(i + 2).join('*');      let edges = Array(spaceNo).join(" ");      towers[i] = edges + stars + edges;      spaceNo -= 1;    }    
return towers;}console.log(towerBuilder(3));


Related Topics



Leave a reply



Submit