Task

Given a prompt and a list containing a number of strings of which one is to be selected, create a function that:

 prints a textual menu formatted as an index value followed by its corresponding string for each item in the list;
 prompts the user to enter a number;
 returns the string corresponding to the selected index number.


The function should reject input that is not an integer or is out of range by redisplaying the whole menu before asking again for a number. The function should return an empty string if called with an empty list.

For test purposes use the following four phrases in a list:

   fee fie
   huff and puff
   mirror mirror
   tick tock

Note

This task is fashioned after the action of the Bash select statement.

$ include "seed7_05.s7i";
 
const func string: menuSelect (in array string: items, in string: prompt) is func
  result
    var string: selection is "";
  local
    var string: item is "";
    var integer: index is 0;
    var integer: num is 0;
  begin
    if length(items) <> 0 then
      repeat
        for item key index range items do
          writeln(index <& ". " <& item);
        end for;
        write(prompt);
        readln(num);
      until num >= 1 and num <= length(items);
      selection := items[num];
    end if
  end func;
 
const array string: items is [] ("fee fie", "huff and puff", "mirror mirror", "tick tock");
const string: prompt is "Which is from the three pigs? ";
 
const proc: main is func
  begin
    writeln("You chose " <& menuSelect(items, prompt));
  end func;