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

I don't understand why the code always hits the "else" statement. The below sample output implies that the pattern and the spilt worked "ok" so could you please point out my error?
foreach $key (keys %analysis) { @msg = (); $val = $analysis{$key}; @msg = split /:/, $val; $anal_host_flg = 0, $anal_data_flg = 0, $anal_entries = 0; $anal_class_flg = 0, $anal_image_flg = 0, $anal_channel_flg = 0; foreach $message (@msg) { print "$message\n"; $anal_entries = $#msg + 1; if ($anal_entries == 0) { print "Program Error 3 - $key $val\n"; } if ($message =~ /SECTION IS NOT IN EADAS CLASS/) { $anal_class_flg = $anal_class_flg + 1; } elsif ($message =~ /Office Image Running/) { $anal_image_flg = $anal_image_flg + 1; } elsif ($message =~ /Office Channel Not Logged In/) { $anal_channel_flg = $anal_channel_flg + 1; } elsif ($message =~ /Telephath Unable To Connect to Office/) { $anal_host_flg = $anal_host_flg + 1; } elsif ($message =~ /No New Data Was Collected/) { $anal_data_flg = $anal_data_flg + 1; } else { print "Program Error 4 - $key $val\n"; } } }
Sample output is below. Please note the value for $key was removed. Program Error 4 - <$key> :Office Channel Not Logged In :No New Data Was Co Office Channel Not Logged In No New Data Was Collected Program Error 4 - <$key> :SECTION IS NOT IN EADAS CLASS :SECTION IS NOT IN SECTION IS NOT IN EADAS CLASS SECTION IS NOT IN EADAS CLASS

Replies are listed 'Best First'.
Re: Is My Split Or My Pattern Bad?
by dragonchild (Archbishop) on Nov 22, 2004 at 17:47 UTC
    1. Enable strict and warnings. That will help clean up a lot of your code.
    2. Your first if-statement will never execute. If $anal_entries is 0, that means $#msg is -1, which means that @msg contains nothing. If that was the case, you would never enter your for-loop.
    3. Your split looks fine, as far as it goes. But ...
    4. Can you create some dummy data, give it to us, and show us what you get when you run it? It's really hard to diagnose errors when we don't have all the info.
    5. I would strongly suggest you read Learning Perl, by Randal Schwartz published by O'Reilly & Associates. It looks like you came from a VisualBasic-type backround and are uncomfortable with Perl as a language.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Here is the dummy data being read in earlier in the code: Mon Nov 8 10:00:13 2004 Eat At Joes For ABCDEFGHILK Mon Nov 8 10:00:36 2004 Eat At Salleys For KLIHGFEDCBA Mon Nov 8 10:00:31 2004 Eat At Joes For EATATJOESOK Mon Nov 8 10:00:39 2004 Eat At Salleys For EATATSALLYS Below is what the code is putting out: Program Error 4 - KLIHGFEDCBA :Eat At Salleys Eat At Salleys Program Error 4 - EATATJOESOK :Eat At Joes Eat At Joes Program Error 4 - EATATSALLYS :Eat At Salleys Eat At Salleys Program Error 4 - ABCDEFGHILK :Eat At Joes Eat At Joes
        (It would help if you put things that have newlines in them between <code> tags.)

        It looks like the problem is in the building of the %nalysis hash. I would start there. Reason I know this - I doubt your keys should have a colon in them. :-)

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Is My Split Or My Pattern Bad?
by ikegami (Patriarch) on Nov 22, 2004 at 17:46 UTC

    The code you provided works fine (as shown below). What input are you feeding it?

    By the way, "anal" is a very poor choice of abbrieviation for "analysis".

Re: Is My Split Or My Pattern Bad?
by steves (Curate) on Nov 22, 2004 at 18:11 UTC

    By the way, "anal" is a very poor choice of abbrieviation for "analysis".

    Glad you said it. $anal_entries is a particularly (ahem) "uncomfortable" variable name. ;-)

      Yes, this was changed to $analysis. Thank you for pointing this out, I sincerely appreciate your help.
        Thats too bad, I thought it was the best variable name I had ever seen...
Re: Is My Split Or My Pattern Bad?
by fglock (Vicar) on Nov 22, 2004 at 17:53 UTC

    Is it possible that split is generating an empty $message that would fall through "Error 4" ?

    perl -e ' print "[$_]\n" for ( split /:/ => ":xxx" ) ' [] <== empty $message [xxx]
      Well, the code was changed to "|" and it seems to work. Thank you!