Storing cutted filenames into variable

Hi,
I'd like to write a script which works on various files file1.jpg, file2.jpg ..... These files are splitted and their names are something like file[1-9]_[1-9].XXX. I'd like to merge them and convert them at some moment again in file[1-9].XXX also
file1_1.jpg file1_2.jpg ... >file1.pdf
file2_1.txt file2_2.txt ... >file2.pdf

I'd use for loop and I need there to specify the variable - the first part of the filename. I'd write
for file in *[#-#]_*.jpg
do for i in {#-#}
do convert .....
done
done

I don't know how many files I'd use for the conversion in advance because it varies, I even't don't know the names file1, file2 and their amount. I'd need to get it know from the folder, somehow count the amount of file1_xxx files and the amount of file[1-9] files.
How can I do that?
Thank you very much!

This would give you a count of the number of files for each different file extension you've got:

ls | perl -p -e 's/.+\.(.+?)/$1/' | sort | uniq -c

As for the loop, you can always use an incremental counter and test if the file exists. Say with .jpg files:

#!/usr/bin/ksh

NUM=1
while [[ -e a1_$NUM.jpg ]];do
  echo "a1_$NUM.jpg exists"
  let  NUM=$NUM+1
done

Don't know if this is what you needed.

GREAT!
Thank you very much!