in reply to SPLIT()ing headache

Use strict and warnings. Correct answers already given, but use strict would've caught it before you even had to ask the question... :-)

Also, why is $n=($#code+1); in the same loop where you have for($n=0;$n<=$#code;$n++), (is it meant to act like last?); seems like a bug or at least confusing to me. You should think about iterating over arrays directly (e.g. for my $element (@array) {...}) and using next, last, or redo if needed for loop control. See the perldocs (perlsyn, perlfunc).

$writer=$tparse['2'];

And you shouldn't/don't need to quote numbers in an array.

Update: Ok so it was meant to be like last (the indentation was confusing). But when code is simple and more clearly reflects its intention, its easier to find the problems.

Here is your code (slightly) improved:

use strict; # I think " " is more what you intend than / / # see the split docs in perlfunc, and then # you also don't need to chomp it. for my $prev (split " ", $input) { print " -> $prev\n"; my $current=$prev; for my $find (@code) { chomp($find); if ($prev=~/$find/i) { print "$blink $cyan -> Logged \n"; print "$normal$green"; #HERE IS THE SPLIT PROBLEM AREA! my $tparse=split(/-/,$current); my $writer=$tparse[2]; my $writes=$#tparse; print "$cyan -> $prev \n $green"; # You had an error here which 'use strict' # also would've caught #print "$cyan -> $#writes \n $green"; print "$cyan -> $writes \n $green"; print "$cyan -> $writer \n $green"; last; } } }

Replies are listed 'Best First'.
Re: Re: SPLIT()ing headache
by PyroX (Pilgrim) on Nov 06, 2001 at 03:29 UTC
    ya ya the code is not perfect, I know, but it works. That jump out of the loop when it gets a result.

    I trying to get more involved with perl,