http://www.perlmonks.org?node_id=1024881


in reply to Limited Number of User Input

It doesn't make much sense why you would ask the user for the number of file names they want to enter, then try to limit the number of files they enter to what they said. Why not just ask the user to enter the filenames in the first place--then there will never be a discrepancy between some number they entered and the number of file names they actually entered?

To limit the number of inputs, you can specify a LIMIT for split:

my $count = 4; my $fnames = "file1 file2 file3 file4 file5 file6"; my @fnames = split ' ', $fnames, $count + 1; if (@fnames > 4) { pop @fnames; } say "@fnames"; --output:-- file1 file2 file3 file4

But what if the user enters fewer names?