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

Using Ubuntu 18.04 the default perl. Have tried to understand this code but not getting expected results. Any help please. tia oldcity

########################################## sub OPEN_200R { open(FILE, '<', 'CATAGORIES.DAT') or die("Could not open* file! +CATAGORIES DAT\n"); @lines = <FILE>; close(FILE); $gcnt = 0; foreach (@lines) { ($catg) = (split(/,/)); $gcnt = $gcnt + 1; chomp $catg; $catg[$gcnt] = $catg; if ($gcnt <= 5) { $catlgs5 = join '', $catg[$gcnt]; }elsif ($gcnt >= 6){ $catlgs6 = join '', $catg[$gcnt]; } # end of if gcnt print ("$catlgs5"); print ("$catlgs6 \n"); # chomp $catg; } # end of foreach at 200r # { ##########################################
Not results Expected. 1-AT&T 2-FP&L 3-WATER 4-GAS 5-PLAN-D 5-PLAN-D 6-AARP 5-PLAN-D 7-DIAZ 5-PLAN-D 8-DRUGS 5-PLAN-D 9-HOUSE 5-PLAN-D10-CAR 5-PLAN-D11-MISC

Had expected 2 rows 1 of 5 catgs and 1 of 6 catgs. How to fix?

Replies are listed 'Best First'.
Re: Unexpected Results
by Athanasius (Archbishop) on Nov 13, 2018 at 03:05 UTC

    Hello oldcity,

    Here’s a guess at what you’re looking for:

    use strict; use warnings; OPEN_200R(); sub OPEN_200R { my @lines = <DATA>; my $gcnt = 0; my @catg; my $catlgs5 = ''; my $catlgs6 = ''; for (@lines) { my ($catg) = split ' '; ++$gcnt; chomp $catg; $catg[$gcnt] = $catg; if ($gcnt <= 5) { $catlgs5 .= '|' . $catg[$gcnt]; } elsif ($gcnt >= 6) { $catlgs6 .= '|' . $catg[$gcnt]; } } print "5: $catlgs5\n"; print "6: $catlgs6\n"; } __DATA__ 1-AT&T 2-FP&L 3-WATER 4-GAS 5-PLAN-D 5-PLAN-D 6-AARP 5-PLAN-D 7-DIAZ 5-PLAN-D 8-DRUGS 5-PLAN-D 9-HOUSE 5-PLAN-D10-CAR 5-PLAN-D11-MISC

    Output:

    12:55 >perl 1942_SoPW.pl 5: |1-AT&T|2-FP&L|3-WATER|4-GAS|5-PLAN-D 6: |5-PLAN-D|5-PLAN-D|5-PLAN-D|5-PLAN-D|5-PLAN-D10-CAR|5-PLAN-D11-MISC 12:55 >

    Notes:

    • Always use strict; and declare variables with my.
    • join has no effect on a single scalar. I think you’re looking to concatenate the existing contents of $catlgs5 with the the current value of $catg[$gcnt]. You could do this: $catlgs5 = join '|', $catlgs5, $catg[$gcnt];, but using the concatenation operator . is simpler.
    • I’ve used | as the separator (rather than the empty string) only in order to make the output easier to read.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Unexpected Results
by ikegami (Patriarch) on Nov 13, 2018 at 02:32 UTC

    Please provide the input data and the desired output.

      Using CATAGORIES.DAT. Thanks hth.
      1-AT&T 2-FP&L 3-WATER 4-GAS 5-PLAN-D 6-AARP 7-DIAZ 8-DRUGS 9-HOUSE 10-CAR 11-MISC

        Is that the contents CATAGORIES.DAT or the desired output? And where's the other one?

Re: Unexpected Results
by Laurent_R (Canon) on Nov 13, 2018 at 20:14 UTC
    Hi oldcity,

    is there anything wrong with using correct indentation and consistent code formatting?

    Please look at Athanasius's code and yours, and ask yourself: which one you would prefer work on if you had to maintain it?

    Code is for humans to read as much as for computers to compile. Compilers may not care about code formatting but, IMHO, consistent indentation is not optional, it is also not aimed at making things look pretty, it is essential for you and others to understand your code. Do yourself a favor: indent your code consistently. BTW, you wouldn't need comments such as # end of foreach at 200r if your code was properly formatted.