Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Limited Number of User Input

by kash650 (Novice)
on Mar 21, 2013 at 23:07 UTC ( #1024837=perlquestion: print w/replies, xml ) Need Help??
kash650 has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to have only a certain number of inputs from the user? For example I ask the user how many files they want to input, then read those file names into an array. I tried doing this but it doesn't work. I know this will read into an array, but it can't limit the number of inputs.

my @arr = split(/\s+/, <>);

This is what I've tried so far but to no success

print "How many files do you want to combine?: "; my $numfiles = <>; print "Please input the file names:\n"; my @filenames; for (my $currentfile = 0; 0 < $numfiles-1; $currentfile++) { my $line = <STDIN>; $line =~ s/(^\s+)|(\s+$)//g; @filenames = split(/\s+/,$line); }

Replies are listed 'Best First'.
Re: Limited Number of User Input
by NetWallah (Canon) on Mar 22, 2013 at 00:00 UTC
    You discovered "chomp".

    The next step is to fix the "for loop" termination condition.

    "0 < $numfiles-1" will not work. Try comparing $currentfile with $numfiles.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: Limited Number of User Input
by graff (Chancellor) on Mar 22, 2013 at 04:13 UTC
    It would appear that this script is intended to be used interactively in a command-line shell. In that context, the typical approach for getting a list of file names from the user is to have the user provide the names on the command line, so that the script gets the list as @ARGV.

    Every shell ever invented supports wildcard characters as a shortcut for specifying sets of file names on a command line, and every decent shell also provides tab completion of file names, so it's a lot easier for the user to supply file names on the command line when running the script. The user doesn't need to answer a question about how many files will be specified, and doesn't need to worry about typing mistakes while the script is running.

    None of the snippets in the OP are necessary if the script can be run from the command-line prompt like this: *.foo # (matches all files with a ".foo" extension" #or: this_long_name.txt that_other_long_name.xml # where hitting <tab> after "thi" and "tha" completes each name automa +tically
    Then the script just looks at @ARGV to know how many files were given, and what their names are:
    #!/usr/bin/perl ... printf "There were %d files mentioned on the command line:\n", scalar +@ARGV; print join "\n", @ARGV, ''; ...
Re: Limited Number of User Input
by kash650 (Novice) on Mar 21, 2013 at 23:36 UTC

    EDIT: found out how to do this, using

    while (my $text = <STDIN>) { chomp($text); print "You entered '$text'\n"; last if ($text eq ''); }

    but with a counter instead of "last"

      Yes, good. May I ask, why do you ask the user to enter the number of files first when you already know the simple, intuitive (and very commonly used) way like this for the user to indicate they're finished? If you need to know how many files they entered, you can do something like this:

      while (<STDIN>) { chomp; last if $_ eq ''; push @files, $_; } printf "You entered %d files: { %s }\n", scalar @files, join(' ', @fil +es);
Re: Limited Number of User Input
by 7stud (Deacon) on Mar 22, 2013 at 06:36 UTC

    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?

Re: Limited Number of User Input
by vinoth.ree (Monsignor) on Mar 22, 2013 at 04:52 UTC

    As you prompted the user the number of file names to combine, then you need to get the filename from the user by entering the filename line by line, not space separated filenames!

    If you get the filenames in a single line separated by space, <STDIN> reads whole content till you press the enter key.

    All is well

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024837]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-05-28 10:48 GMT
Find Nodes?
    Voting Booth?