First: @_[0] is "legit", in that it is a 1 element slice of the array @_.. but most likely is not at all what you really want.

When an array is passed as a parameter to a subroutine in perl, it is "unrolled". In other words, each element of the array is passed as a single parameter. In order to pass an array to a subroutine as an array, you must pass a *reference* to the array instead.

Since your subroutine uses only one array plus a single character as parameters, I see two obvious choices here. One of them would be to pass a reference to the array as a parameter, and the character another. The other would be to pass in the character as the first parameter, and treat the rest of the parameters as the array to be searched.

So, please take a peek at this:

<code>

#!/usr/bin/perl

  use strict;
  use warnings;


  # Option 1: using an array ref

  sub search1 {
    my ($array_ref, $char) = @_;       # Grab the parameters
    foreach my $val (@$array_ref) {    # Note the '@$' : This dereferences
      return 1 if $char eq $val;       #   the array
    }
    return -1;
  }

  # Option2: pass in the array "unrolled"

  sub search2 {
    my $char = shift;          # Now the values to be search are in @_
    foreach my $val (@_) {     # note that we are searching @_
      return 1 if $char eq $val;
    }
    return -1;
  }

  my @test_array = qw{ a b c d e f g};

  print "Character 'z' is in the array\n"
    if search1(\@test_array, 'z') == 1;
  print "Character 'z' is not in the array\n"
    if search1(\@test_array, 'z') == -1;

  print "Character 'a' is in the array\n"
    if search1(\@test_array, 'a') == 1;
  print "Character 'a' is not in the array\n"
    if search1(\@test_array, 'a') == -1;


  print "Character 'z' is in the array\n"
    if search2(\@test_array, 'z') == 1;
  print "Character 'z' is not in the array\n"
    if search2(\@test_array, 'z') == -1;

  print "Character 'a' is in the array\n"
    if search2(\@test_array, 'a') == 1;
  print "Character 'a' is not in the array\n"
    if search2(\@test_array, 'a') == -1;

</code>

Best of luck!

Nathan



On 03/12/2014 10:15 AM, Jing Yu wrote:
Is @_[0] even legit?
On 12 Mar 2014, at 04:58, Alex Chiang <pigfly...@gmail.com <mailto: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


Reply via email to