On Wed, Sep 26, 2001 at 12:49:43AM +0200, birgit kellner wrote:
> my @hitsarray = qw(this is the first hit this is the second hit this is the 
> third hit);
> my $fieldnumber = "4";
> my $searchposition = "2";
> I want to know whether @hitsarray contains two sets of $fieldnumber 
> elements where the string at $searchposition is identical - in this 
> example, that would be "is".

It would?  Your variables are off by one.  Element 2 in @hitsarray is 'the',
not 'is'.  $fieldnumber is set to 4, but you clearly have groupings of 5
words:

    @hitsarray = qw(
        this is the first  hit
        this is the second hit
        this is the third  hit
    );


Also, $fieldnumber seems to me misnamed; it should be a length, not a
number.

So, given that, one solution is:

    $excerpt_len = 5;
    $search_pos  = 1;

    @hitsarray = qw(
        this is the first  hit
        this is the second hit
        this is the third  hit
    );

    for (my $i = 0; $i < @hitsarray; $i += $excerpt_len) {
        my $val = $hitsarray[$i + $search_pos];
      
        for (my $j = $i + $excerpt_len; $j < @hitsarray; $j += $excerpt_len) {
            if ($hitsarray[$j + $search_pos] eq $val) {
                print(
                    "Match! ",
                    "\"@hitsarray[$i .. $i + $excerpt_len - 1]\" and ",
                    "\"@hitsarray[$j .. $j + $excerpt_len - 1]\"\n",   
                );
            }     
        }
    }

See?  It's a simple matter of math, using indices rather than manually
splitting up the array.

Now, I can't help but notice this @hitarray looks very much like the array
resulting from your last question; why aren't you pushing array references,
as I suggested?  It would make this much easier.

Given the oddity of this request, and the previous request, what is it this
whole thing is intended to accomplish?


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to