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

Hi, I am reading a file and fetching only the records which matches the condition. I am using the following code block to do the matching. This works fine for the 1st iteration of the outer loop. after that point its just not working .
my $arrayref =\@array; my @arraynew; foreach $key_pos(keys(%conwithposition)) { #print "KEYLOOP::(@{$conwithposition{$key_pos}}[1],@{$conwithposit +ion{$key_pos}}[2])\n";#,$line[@{$conwithposition{$key_pos}}[0]])\n"; $i=0; #print "ARR:@arraynew\n"; file:for (@$arrayref) { my $match = 0; my @line = split /\t/; #print "Line:::@$arrayref\n"; my @tmp; my ($op, $arg1, $arg2) = (@{$conwithposition{$key_pos}}[1],@{$ +conwithposition{$key_pos}}[2],$line[@{$conwithposition{$key_pos}}[0]] +); $match = is_match($op,$arg2, $arg1); if ($match){ push @tmp,@line[@sorted_cols]; print "@tmp\n"; } push @arraynew,[@tmp]; $i++; } $arrayref = \@arraynew; } print "Result: @tmp\n"; sub is_match{ my ($operator,$valuesrc,$inputval_ref)=@_; my $i; if (ref $inputval_ref eq 'SCALAR'){ return 1 if ($valuesrc eq $$inputval_ref); } if (ref ($inputval_ref) eq 'ARRAY'){ my @valuetomatch=@$inputval_ref; for $i(0 .. $#valuetomatch){ return 1 if ($valuesrc eq $valuetomatch[$i]); } } return 0; }
Can anybody correct my mistake? Thanks

Replies are listed 'Best First'.
Re: Looping array sequence
by Corion (Patriarch) on Dec 08, 2006 at 10:53 UTC

    "Just not working" is not a very accurate description of what happens. Please post a small snippet of input and output data. Also, we don't know what %conwithposition is and what values it contains. Please post a small self-contained but complete program that exhibits the same problem.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Looping array sequence
by swampyankee (Parson) on Dec 08, 2006 at 18:23 UTC

    What, explicitly and exactly, are you trying to do? (Think of this as an essay question upon which you will be graded for clarity and style).

    Also, when one is asking a question where there are problems associated with data, it's quite helpful to include data which show the failure. From your description, I cannot be certain whether %conwithposition has more than one key.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: Looping array sequence
by tinita (Parson) on Dec 09, 2006 at 11:33 UTC
    my ($op, $arg1, $arg2) = (@{$conwithposition{$key_pos}}[1],@{$conwithp +os +ition{$key_pos}}[2],$line[@{$conwithposition{$key_pos}}[0]]);
    just a comment on style, i find this extremely unreadable.
    my ($op, $arg1, $arg2) = ( @{ $conwithposition{$key_pos} }[1,2], $line[ $conwithposition{$key_pos}->[0] ] );
Re: Looping array sequence
by Util (Priest) on Dec 10, 2006 at 04:13 UTC

    The OP was reposted, along with additional details, under the author's real ID here. I have posted a (probable) solution in that thread.