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

ok, what the heck is the problem here,,,,

-> Sample text I need to split: '%DAT-3-12123'
-> Problems encountered, the array count after the split is -1, its not splitting.

$input = $_; @format=split (/ /, $_); for ($i=0; $i<=$#format; $i++) { print " -> $format[$i]"."\n"; $prev=$format[$i]; for($n=0;$n<=$#code;$n++) { $find=$code[$n]; chomp($find); chomp($prev); $current=$prev; if ($prev=~/$find/i){ print "$blink $cyan -> Logged \n"; print "$normal$green"; #HERE IS THE SPLIT PROBLEM AREA! $tparse=split(/-/,$current); $writer=$tparse['2']; $writes=$#tparse; print "$cyan -> $prev \n $green"; print "$cyan -> $#writes \n $green"; print "$cyan -> $writer \n $green"; $n=($#code+1); } } }

Replies are listed 'Best First'.
Re: SPLIT()ing headache
by runrig (Abbot) on Nov 06, 2001 at 00:44 UTC
    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; } } }
      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,
(Ovid) Re: SPLIT()ing headache
by Ovid (Cardinal) on Nov 06, 2001 at 00:33 UTC

    This:

    $tparse=split(/-/,$current);

    Should be:

    @tparse=split(/-/,$current);

    Since, in the line after the split, you are trying to access $tparse['2'], it's clear that you meant to split to an array. You attempted to split to a scalar. When you're done, $tparse will be a scalar set to the number of items that the split produced.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: SPLIT()ing headache
by Elliott (Pilgrim) on Nov 06, 2001 at 00:34 UTC
    Hey - one I can answer!!

    $tparse=split(/-/,$current);

    should be

    @tparse=split(/-/,$current);
Re: SPLIT()ing headache
by kwoff (Friar) on Nov 06, 2001 at 00:36 UTC
    I think the (or at least a) relevant part of your code is:
    #HERE IS THE SPLIT PROBLEM AREA! $tparse=split(/-/,$current); $writer=$tparse['2'];
    $tparse is a scalar, so split will return a number, like 3.

    `perldoc -f split`

    ... In scalar context, returns the number of fields found and splits into the "@_" array. Use of split in scalar context is deprecated, however, because it clobbers your subroutine arguments.

Re: SPLIT()ing headache
by PyroX (Pilgrim) on Nov 06, 2001 at 00:36 UTC
    OMG, I have been using PHP too long, thanks guys
    *smacks forehead*