my @records = qw(1 2 3 4 5 6); @input::id = qw(id1 id2 id3 id4); @input::type = qw(document person); print summarise(\@input::id, \@input::type, scalar(@records)); exit; sub summarise { my ($ids,$types,$matches) = @_; my $sentence = 'We found '; $sentence .= $matches || 'no'; if (@$types) { foreach my $i (0..$#$types) { if ($i && $i == $#$types) { $sentence .= ($matches > 1) ? ' and ' : ' or '; } elsif ($i) { $sentence .= ', '; } # document types are in the database # with singular and plural forms of their title # but i've skipped that part here $sentence .= qq| $$types[$i]|; } } else { $sentence .= "item"; $sentence .= "s" if ($matches > 1); } $sentence .= ' relevant to '; $sentence .= 'both ' if (@$ids == 2); $sentence .= 'all of ' if (@$ids > 2); foreach my $i (0..$#$ids) { if ($i && $i == $#$ids) { $sentence .= ' and '; } elsif ($i) { $sentence .= ', '; } # keyword titles also looked up from the database really. $sentence .= qq|$$ids[$i]|; } return $sentence; }