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

hi,
good morning,

I've done one program to read this file, please refer as below:

NAME SUM ITEMS_A ITEMS_B ITEMS_C BAA 1 1348396499 134000 BAA 10 1333333333 002333 ITC 9 1340014296 181818 ITC 10 1349999999 134110 BAA 29 1346512572 181919 BAA 100 1349990990 * FAC 3 1346048490 134795 122233 FAC 10 1346666655 181919 130303 BAB 9 1345100000 181919 ...
But, there are too long and not very good. So, anybody knows how to make it easier and simple. The program algorithm are:
If NAME =~ /BAA/ if ITEMS_A =~ /^134/ AND ITEMS_B =~ /^134/ THEN TBAA+=SUM a++; elsif ITEMS_A =~ /^134/ AND ITEMS_B !~ /^(134|00)/ THEN OBAA+=SUM b++; elsif ITEMS_A =~ /^134/ AND ITEMS_B =~ /^00/ THEN IBAA+=SUM ab++; else EBAA+=SUM c++; elsif NAME =~ /ITC/ if ITEMS_A =~ /^134/ AND ITEMS_B =~ /^134/ THEN TITC+=SUM d++; elsif ITEMS_A =~ /^134/ AND ITEMS_B !~ /^134/ THEN OITC+=SUM e++; else EITC+=SUM f++; elsif NAME =~ /FAC/ if ITEMS_A =~ /^134/ AND ITEMS_B =~ /^134/ THEN if ITEMS_C !~ /130303/ THEN TFAC+=SUM g++; else TXFAC+=SUM h++ elsif ITEMS_A =~ /^134/ AND ITEMS_B !~ /^134/ THEN if ITEMS_C !~ /130303/ THEN TYFAC+=SUM i++; else TZFAC+=SUM j++ else if ITEMS_A =~ /^134/ AND ITEMS_B =~ /^134/ THEN UNK+=SUM k++; elsif ITEMS_A =~ /^134/ AND ITEMS_B !~ /^134/ THEN UNK2+=SUM l++; else UNK3+=SUM m++;
finally, print all the result by following the NAME, as example:
NAME TOTAL_SUM TOTAL BAA (134->134) 140 5 (134->other) 123 4 (134->00???) 20 1 (134->unknown) 0 0 ITC (134->134) 100 10 (134->other) 30 4 (134->unknown) 0 0 FAC (134->134->130303) 30 3 (134->134->other) 90 4 (134->other->130303) 60 2 (134->other->other) 1 1 OTH (134->134) 10 1 (134->other) 9 1 (134->unknown) 1 1

20030717 Edit by jeffa: Changed title from 'how to make it easier ?'

Replies are listed 'Best First'.
Re: Help with simplifying file parsing program?
by robartes (Priest) on Jul 17, 2003 at 05:58 UTC
    Since this does indeed smell of homework (and I'm lazy), I'll not provide a shrink wrapped solution (it is supposed to be homework after all). But have a look at pack to parse the lines and have a closer look than you have before at perlre to interpret them. Also, a book like Learning Perl or yea, even an extended stroll through this very site, will help you even more.

    Good luck!

    CU
    Robartes-

      Why is it that now I only have work work, homework looks fun?

      I would look at arrays and sorting as possibilities, or if you feel daring hashes.

      have fun!
      John

Re: Help with simplifying file parsing program?
by waswas-fng (Curate) on Jul 17, 2003 at 05:22 UTC
    Do I get credit in your class for doing it for you? =)

    -Waswas
Re: Help with simplifying file parsing program?
by Skeeve (Parson) on Jul 17, 2003 at 06:03 UTC
    You could outfactor(?) the combined ifs:
    If NAME =~ /BAA/ if ITEMS_A =~ /^134/ if ITEMS_B =~ /^134/ THEN TBAA+=SUM a++; elsif ITEMS_B !~ /^(134|00)/ THEN OBAA+=SUM b++; elsif ITEMS_B =~ /^00/ THEN IBAA+=SUM ab++; else EBAA+=SUM c++; # AFAICS this will never be reached. # Either ITEMS_B starts wit 134 or 00 or something # else. The last case is targeted by your second # ITEMS_B-if
    To shorten it, or at least make it more switch-like, in perl you could use the aliasing to $_ or "for":
    SWITCH: for ($ITEMS_B) { /^134/ && do { # see above last SWITCH; }; !/^(134|00)/ && do { # see above last SWITCH; }; /^00/ && do { # see above last SWITCH; }; # else do { #see above last SWITCH; } }