Home > Archive > PERL Beginners > July 2006 > more concise
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ryan Dillinger 2006-07-29, 7:57 am |
| Hello All;
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";
| |
| kenslaterpa@hotmail.com 2006-07-29, 7:57 am |
|
Ryan Dillinger wrote:
> Hello All;
> 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";
First, you have committed the unpardonable sin of not having a "use
strict;"
statement at the top of your program. If you had added that and
declared the variables
you would see that you misspelled $IndexLocation.
You did not tell us what error you were getting, but I assume it had
something to do with the misspelling.
Now to being concise:
> $SearchString = "";
This line is not needed, $SearchString is set below
> print "\nType the string \"in\" to begin a search: " . $SearchString;
Why is $SearchString appended to the end?
>
> $SearchString = <STDIN>;
Declare $SearchString here :
my $SearchString =<STDIN>;
> chomp($SearchString);
> print "\nSearch begun for \"" . $SearchString . "\"...";
No need to concatenate here, I find it harder to read:
print "\nSearch begun for \"$SearchString \"...";
> my $IndexLocation = index($TargetString , $SearchString, 0);
> print "\nString found at index " . $IndexLocation . "...";
>
You perform the index function again later to find the same string.
This calls for a loop, as you don't always know you will have
only two occurrences. Since index returns a -1 on failure, use that
to stop the loop.
my $IndexLocation = 0;
while (($IndexLocation = index($TargetString , $SearchString,
$IndexLocation)) > -1)
{
print "\nString found at index $IndexLocation...";
$IndexLocation++;
}
Others will probably come up with ways to streamline it wven more,
but I hope this helps.
Ken
Rewritten script:
use strict;
use warnings;
print (my $TargetString = <<TARGET);
The intellect of man is in...
.........etc..
TARGET
my $SearchString = "";
print "\nType the string \"in\" to begin a search: ";
$SearchString = <STDIN>;
chomp($SearchString);
print "\nSearch begun for \"$SearchString \"...";
my $IndexLocation = 0;
while (($IndexLocation = index($TargetString , $SearchString,
$IndexLocation)) > -1)
{
print "\nString found at index $IndexLocation...";
$IndexLocation++;
}
| |
| Rob Dixon 2006-07-29, 7:57 am |
| 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
| |
| Rob Dixon 2006-07-29, 7:57 am |
| Ryan Dillinger wrote:
> Hello All;
> 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";
I forgot to mention: you've misspelled $IndexLocation in the second call to
index(). Your code gives a warning to this effect as it stands, but you seem not
to have noticed it? Using strict forces you to declare all the variables you use
and would have caught this without even trying to run the program.
Rob
|
|
|
|
|