c_lhee has asked for the wisdom of the Perl Monks concerning the following question:

The SCRIPT BELOW works, when listing and comparing each array.
#list $array_lenth = @array = ("perl.a", "perl.b", "perl.c"); $array_lenth = $array_lenth -1; for ($i = 0; $i <=$array_lenth; $i++) { $element = $array[$i]; print "--value of $i element = $element \n"; #values to match @TheArray = qw(perl.a perl.b perl.c); if (grep {$_ eq $element} @TheArray) { print qq(<td>Found '$element' element!</td>\n); $element =[$_]; } else { print "NOT FOUND '$element' element!\n" ; $element =[$_]; }; }
HOWEVER, if the @array and @TheArray were substituted with the values of
$dummy_a = $input{'dummy_a'};
which is now
$dummy_a = "perl.a perl.b perl.c perl.d perl.e";
THEN, set as
@array = $dummy_a; or @TheArray = $dummy_a;
THE list in $dummy_a is recognized as a value from one element only...Therefore I cannot count and compare the list. Why is that. c_lhee@yahoo.com

Replies are listed 'Best First'.
•Re: Comparing and Counting Arrays Length
by merlyn (Sage) on Apr 08, 2003 at 07:23 UTC
      it worked.. why do i have to include this statement --split ' ', ---what does this do.
      @array1 = split ' ', $input{'dummy_a'}; $array_lenth = @array = (@array1);
      c_lhee@yahoo.com
        If you do:
        @array1 = "foo bar baz";

        Then you have just one thing on the right hand side. Hence, you only get one thing on the left hand side, so @array1 contains just one element.

        The split function, as documented in the manual page, takes a regex and a string as argument, and splits the string in several parts, based on the regex. An argument of " " is special, and splits the string on whitespace. Splitting "foo bar baz" on whitespace gives you a list of three strings, hence the array will contain three elements.

        Abigail

Re: Comparing and Counting Arrays Length
by c_lhee (Novice) on Apr 08, 2003 at 09:07 UTC
    it worked.. why do i have to include this statement --split ' ', ---what does this do.
    @array1 = split ' ', $input{'dummy_a'}; $array_lenth = @array = (@array1);
    c_lhee@yahoo.com