Ryan Dillinger wrote:
>
> I was hoping to write this script in a more concise manner, also
> eliminate the error I get with it, along with correcting it indexing at
> only one occurence. Thanks for your help!
>
> #!/usr/bin/perl
> use warnings;
>
> print ($TargetString = <<TARGET);
> The intellect of man is in...
> ........etc..
> TARGET
>
> $SearchString = "";
> print "\nType the string \"in\" to begin a search: " . $SearchString;
>
> $SearchString = <STDIN>;
> chomp($SearchString);
> print "\nSearch begun for \"" . $SearchString . "\"...";
>
> my $IndexLocation = index($TargetString , $SearchString, 0);
> print "\nString found at index " . $IndexLocation . "...";
>
> $IndexLocation = ($IndexLocation + 1);
> $IndexLocation = index($TargetString, $SearchString, $IndexLcation);
> print "\nString found at index " . $IndexLocation . "...\n";

Hi Ryan

Most of the bulk of your code is in the verbose output strings and huge variable
names. Removing those and using a regex instead of index() produces this:

  use strict;
  use warnings;

  print (my $tgt = <<TARGET);
  The intellect of man is in...
  ........etc..
  TARGET

  chomp(my $str = <STDIN>);

  print $-[0], "\n" while $tgt =~ /\Q$str/g;

which I think does what you intended. If you really need those strings you can
put them back!!

(By the way, always use strict - even though it does add a line of code!)

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to