gzayzay has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am wondering if anyone could kindly help me with certain info. I have programmed in C++ a little and the getline() function allow users to enter multiple inputs. It is in this view I am wondering whether Perl has similar function that allows users to enter multiple inputs. What i am trying to do is, instead of writing:

print "Enter word1: >> "; chomp (my $word1 = <SDTIN>); print "\nEnter word2: >> "; chomp (my $word2 = <SDTIN>);

I will like to find a means in which the users can enter multiple words at once i.e john, cat, mary, paul. I will like to get the entire entry and do a search word by word. Thus, if any monk know an in-built function that can do that, plese help me out.

Thanks,

gzayzay.

Replies are listed 'Best First'.
Re: Getting Multiple inputs
by McDarren (Abbot) on Apr 05, 2006 at 17:11 UTC
    Just have them enter the words all on one line using the delimiter of your choice - say a space - and then split the line into it's component words. Like so:
    print "Enter some words (separated by spaces): >> "; chomp(my $line = <STDIN>); my @words = split /\s+/, $line;
    Note that the "\s+" will split on one or more whitespace characters, so it will still work if they accidentally type two spaces between some words.

    Cheers,
    Darren :)

      split /\s+/
      would probably be even better as
      split ' '

      >perl -e "@f = split(' ', ' a b c '); print scalar @f 3 >perl -e "@f = split(/\s+/, ' a b c '); print scalar @f 4

      ' ' does the same thing as /\s+/, but it ignores leading spaces.

      split's documentation

      And without the temp variable
      chomp(my @words = (split /\s+/,(<STDIN>)));

      Cheers

      Yeah, I know I'm paren happy

      Sweetblood

      Thanks for the tip.
Re: Getting Multiple inputs
by eric256 (Parson) on Apr 05, 2006 at 17:55 UTC

    Another solution is to use a loop and push the words into an array as you get them. The user enters a blank line to stop entering words. This had the advantage that the user can input complete phrases.

    use strict; use warnings; my @words; my $i = 1; while (1) { print "Enter word $i: >> "; $i++; chomp (my $word = <>); last unless length $word; push @words, $word; } print join(",", @words);

    ___________
    Eric Hodges
      Thanks for te assistance, I will condiserd both methods

      Thanks

      gzayzay,

Re: Getting Multiple inputs
by johngg (Canon) on Apr 05, 2006 at 20:06 UTC
    This should work.

    use strict; use warnings; our @linesEntered = (); our $prompt = "Enter text (Ctrl-D to quit): "; our $dePrompt = "\r" . " " x (length($prompt) + 2) . "\r"; while(1) { print $prompt; last if eof STDIN; chomp($_ = <STDIN>); push @linesEntered, $_; } print $dePrompt; foreach my $line (@linesEntered) { # Do something with the line. ... }

    The script will keep prompting for lines until it gets an EOF on STDIN (Ctrl-D on *nix, Ctrl-Z on Windoze I think but I'm not sure and you'd want to change the prompt). The $dePrompt just erases the prompt to make things look tidy.

    I hope this is of use.

    Cheers,

    JohnGG