in reply to Searching for strings specified via user input

You could do at least two things:

1) Introduce additional command-line arguments, allowing the user to specify a pattern to search for on the command line when invoking the script. Depending on the size of your input file(s) and on the number of searches you want to perform on each, this may be inefficient.

If you do want to do this, and if you eventually want to add more options, you could look into the Getopt family of modules, e.g. Getopt::Std or Getopt::Long.

2) Add a loop where you present a prompt, read a pattern from STDIN and then search for that, something along the lines of the following:

while(1) { print "Enter a pattern to search for, or QUIT to quit >"; $_ = <STDIN>; m/^QUIT$/ and exit(0); search_for_pattern($_); }

With a suitable search_for_pattern(), of course. HTH!

Replies are listed 'Best First'.
Re^2: Searching for strings specified via user input
by TJCooper (Beadle) on May 01, 2014 at 11:59 UTC
    Thank you! I have taken your example and adapted it to my own purposes. I was wondering how I could go about using a space as the delimiter for each search string i.e. in my original code I would specify each substring via: my @pats=qw( GATCR GGCC ); However I now need the user to input each substring with a space in between each and for the script to treat each one as a separate search to carry out. Thanks again!

      You're welcome! Just to avoid unnecessary confusion, when the user enters multiple strings, are you interested only in matches that match all these, or all matches that match any of these?

      Either way you'd want to split the user-supplied search string along whitespace. I'd do that in your hypothetical search_for_pattern routine; in the latter case, you could also do it after the user supplied a search string, and then use a loop to call search_for_pattern for each individual search term, but this could be inefficient if you have a lot of data to deal with.