G'day ahjohnson2,
As 2teez indicated, you really should be using Text::CSV, or one of its related modules. Can you guarantee that none of your data (from now until the end of time) will ever contain a comma or have any other issue related to parsing CSV files?
You appear to have a logic error in the code you posted. If $last eq $second is TRUE and $second eq $first is FALSE, then "$last only = $first\n" will not be a true statement; however, "$last only = $second\n" would be a true statement. Also, in "$second really = $first\n", all three variables would be equal. I'm somewhat lost as to what you're actually attempting to report with this code.
In order to group the printing of the results of your conditions, you could save the tested data in an AoAoA (see "perldsc - Perl Data Structures Cookbook" if that's unfamiliar to you) which looks like this:
( [ # data matching first condition [ $first, $second, $third ], [ $first, $second, $third ], ..., ], [ # data matching second condition [ $first, $second, $third ], [ $first, $second, $third ], ..., ], )
You could then set up another array with rules on how to deal with the data matching each of those conditions.
This script is intended to show how to group the printing. I've shown a fairly simple set of rules as well as a more complex set. I'll leave parsing the CSV data and fixing the logic errors to you.
#!/usr/bin/env perl -l use strict; use warnings; my @print_data; for (sort <DATA>) { chomp; my ($first, $second, $third) = split /,/; next if $second ne $third; push @{$print_data[$first ne $second]}, [ $first, $second, $third +]; } { print '*** Simple Formatting Rules ***'; my @rules = ( "%s (1st) equals %s (2nd) and %2\$s (2nd) equals %s (3rd)\n", "%s (1st) doesn't equal %s (2nd) but %2\$s (2nd) does equal %s + (3rd)\n", ); for my $i (0 .. 1) { printf $rules[$i], @$_ for @{$print_data[$i]}; } } { print '*** More Complex Formatting Rules ***'; my @rules = ( [ "%s (3rd) equals both %s (1st) and %s (2nd)\n", [ 2, 0, 1 +], ], [ "%s (3rd) only equals %s (2nd)\n", [ 2, 1 ], ], ); for my $i (0 .. 1) { printf $rules[$i][0], @{$_}[@{$rules[$i][1]}] for @{$print_dat +a[$i]}; } } __DATA__ 2,2,3 2,2,2 2,1,1 1,2,3 1,2,2 1,1,1
Output:
*** Simple Formatting Rules *** 1 (1st) equals 1 (2nd) and 1 (2nd) equals 1 (3rd) 2 (1st) equals 2 (2nd) and 2 (2nd) equals 2 (3rd) 1 (1st) doesn't equal 2 (2nd) but 2 (2nd) does equal 2 (3rd) 2 (1st) doesn't equal 1 (2nd) but 1 (2nd) does equal 1 (3rd) *** More Complex Formatting Rules *** 1 (3rd) equals both 1 (1st) and 1 (2nd) 2 (3rd) equals both 2 (1st) and 2 (2nd) 2 (3rd) only equals 2 (2nd) 1 (3rd) only equals 1 (2nd)
-- Ken
In reply to Re: Printing Array Output Outisde foreach
by kcott
in thread Printing Array Output Outisde foreach
by ahjohnson2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |