#!/usr/bin/perl -w use strict; use warnings; print "ARGV: $#ARGV\n"; if ($#ARGV != 0) { print "One, and only one (not $#ARGV), command line parameter is expected\n"; exit; } my $inputFile=$ARGV[0]; if ( not -e $inputFile) { die "$inputFile doesn't exist!!!\nExiting.\n"; } print "Attempting to open $inputFile\n"; open my $file, $inputFile or die "Could not open $inputFile: $!"; my $headerLine= -1; my $prtIndex = -1; my $shpIndex = -1; my $clrIndex = -1; my $sizIndex = -1; my %values = (); my $index = 0; my @parts = (); while( my $line = <$file>) { if ( $line =~ "Part *Shape *Color *Size" ){ if($headerLine < 0){ $headerLine=$.; $prtIndex=index $line, "Part"; $shpIndex=index $line, "Shape"; $clrIndex=index $line, "Color"; $sizIndex=index $line, "Size"; } next } if(not checkIndices($prtIndex, $shpIndex, $clrIndex, $sizIndex)){ $headerLine=-1; next; } if(length($line)<$sizIndex){ next; } my $prt = substr $line, $prtIndex, $shpIndex-1-$prtIndex; my $shp = substr $line, $shpIndex, $clrIndex-1-$shpIndex; my $clr = substr $line, $clrIndex, $sizIndex-1-$clrIndex; my $siz = substr $line, $sizIndex; trimall($prt,$shp,$clr,$siz); my $superstr=$shp.$clr.$siz; if(not exists $values{$superstr}){ $values{$superstr}=$index; $parts[$index][0][0]=$prt; #List of all parts $parts[$index][1]=$superstr; #Value of the parts $parts[$index][2]=1; #Total qty of these parts $index++; } else { my $i=$values{$superstr}; $parts[$i][0][$parts[$i][2]] = $prt; $parts[$i][2]++; } } for my $i (0..$index-1){ my @plist=$parts[$i][0]; my $value=$parts[$i][1]; my $count=$parts[$i][2]; for my $j (0..$parts[$i][2]-1){ printf $parts[$i][0][$j]." "; } printf "- "; print $value." - "; print $count.".\n"; } exit; # Check if the indices for a number of input variables are all >= 0 sub checkIndices { my $count; for ($count=0; $count<=$#_; $count++){ if($_[$count] < 0){ return (1==0); } } return (1==1); } # Remove leading and trailing whitespace from all arguments sub trimall { my $count; for ($count=0; $count<=$#_; $count++){ $_[$count] =~ s/^\s*//; $_[$count] =~ s/\s*$//; } return; }