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; } } }

In reply to Re: SPLIT()ing headache by runrig
in thread SPLIT()ing headache by PyroX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.