On 26/10/26 at 19:38, Greg Wooledge wrote:
On Mon, Oct 26, 2026 at 19:21:48 +0100, Franco Martelli wrote:
As usual thanks Greg for the clarifications. I'm still playing with the RNG
code of the script that shuffle a playlist, since it depends on a list of
filenames in a given directory, I'd like now that the indexed array
"playlist" be populated by a sequence of numbers instead of filenames, this
works:

playlist=({001..136})

I don't understand the need for this array, but hold that thought.

but the whole sequence is expanded on a single line. I'd like that each
number be separated by newline \n.

printf '%d\n' "${playlist[@]}"

I still don't know why you want that output, though.

Now, back to the original thought.  If you've got a playlist array
that contains filenames, then that array is *already* numerically
indexed.

playlist=(clip1 clip2 clip3 clip4)
for i in "${!playlist[@]}"; do
     printf 'Index %d is "%s"\n' "$i" "${playlist[i]}"
done

Running this code gives us:

Index 0 is "clip1"
Index 1 is "clip2"
Index 2 is "clip3"
Index 3 is "clip4"

So, from what I can see, you only want to make two changes:

   1) Count starting from 1 instead of 0.
   2) Zero-pad each index to 3 digits.

printf plus the shell's builtin arithmetic allows us to do this easily:

playlist=(clip1 clip2 clip3 clip4)
for i in "${!playlist[@]}"; do
     printf 'Index %03d is "%s"\n' "$((i+1))" "${playlist[i]}"
done

That gives us:

Index 001 is "clip1"
Index 002 is "clip2"
Index 003 is "clip3"
Index 004 is "clip4"

Is that all you needed?

not output, as input to populate the array, John Doe suggested:

playlist=($(printf "%d\n" {001..136}))

but it doesn't work, when I run the script I got these error messages:

~$ ./rndclips
./rndclips: line 9: printf: 008: invalid octal number
./rndclips: line 9: printf: 009: invalid octal number
./rndclips: line 9: printf: 018: invalid octal number
./rndclips: line 9: printf: 019: invalid octal number
./rndclips: line 9: printf: 028: invalid octal number
./rndclips: line 9: printf: 029: invalid octal number
./rndclips: line 9: printf: 038: invalid octal number
./rndclips: line 9: printf: 039: invalid octal number
./rndclips: line 9: printf: 048: invalid octal number
...

This because I need to make the script the more generic as possible therefore I don't want it depends anymore on the list of files on my hard disk. A viable solution could be to use "echo" via pipe with "tr", right?

Kind regards,
--
Franco Martelli

Reply via email to