Mornin' --

Take a look at the index() function, unless you have a real need to
reinvent one of the Perl builtin functions.  (i.e. your home work
assignment from your tescher demands it.)

B


On Tue, Mar 11, 2014 at 9:58 PM, Alex Chiang <pigfly...@gmail.com> wrote:

> Hi there,
>
> I got a wired bug with the following perl script:
>
> 35 # return non-negative value if particular character is in string array
> 36 # otherwise, return -1
>
> sub is_in_string {
> 38 # @s: string array, $c: character
> 39 # passing array into sub
> 40 my @s = @_[0]; my $c = $_[1];
> 41 for my $i (@s) { if ($c eq $i) {return 1} }
> 42 return -1;
> 43 }
> 44 my @ar = qw(t d s);
> 45 my $c = "d";
> 46 my $res = &is_in_string( @ar, $c);
> 47 print $res;
>
> I would expect the result to be 1, but whenever the $c changes, this
> subroutine always return -1.
> Can anyone help me out of this?
>
> Btw, is there any way to search one character against sliced string? I can
> only think of one way as following:
> 1. convert string to array with "split" function
> 2. search one character against sliced array e.g. array[2..$#array]
>
> However, if I want to do the above task multiple times, meaning I've to
> convert string into array every time, pain in the ass...
>
> ---
> Regards !
>
> Alex Chiang
>



-- 

Bob Goolsby
bob.gool...@gmail.com

Reply via email to