First Thread!
Folks,
I have a script that is using the diamond operator to read two files that I have specified at the command line. The first file is a standard text, the second file is a CSV file, with a header. My script works if I remove the header before executing, but I would like to write some logic that will ignore or remove the first line of the second file at run time, so I am not thinking about it.
I run the script this way:
./script.pl -s ./sales-report ./inventory.csv
Body of script
my %sold;
if ( @ARGV > 2 && shift eq "-s" )
{
my $sales_file = shift;
my @saved_argv = @ARGV;
@ARGV = $sales_file;
while ( defined( my $line = <> ) )
{
chomp $line;
my ($quantity, $item) = split " ", $line, 2;
$sold{$item} = $quantity;
}
@ARGV = @saved_argv;
}
else
{
die "Usage: $0 -s sales_file inventory file...\n";
}
my (%item_cost, %inventory);
while ( defined( my $line = <> ) )
{
chomp $line;
my ($cost, $quantity, $item) = split ",", $line, 3;
$item_cost{$item} = $cost;
$inventory{$item} = $quantity;
}
foreach my $item ( keys %sold )
{
if ( exists $inventory{$item} )
{
$inventory{$item} -= $sold{$item};
}
else
{
warn "*** Sold $sold{$item} of $item, which were not in inventory\n";
}
}
foreach my $item ( sort keys %item_cost )
{
printf "%5.2f %6d %s\n", $item_cost{$item}, $inventory{$item}, $item;
}
Any help would be appreciated, I have looked long enough that I fear I am over thinking this....
Folks,
I have a script that is using the diamond operator to read two files that I have specified at the command line. The first file is a standard text, the second file is a CSV file, with a header. My script works if I remove the header before executing, but I would like to write some logic that will ignore or remove the first line of the second file at run time, so I am not thinking about it.
I run the script this way:
./script.pl -s ./sales-report ./inventory.csv
Body of script
my %sold;
if ( @ARGV > 2 && shift eq "-s" )
{
my $sales_file = shift;
my @saved_argv = @ARGV;
@ARGV = $sales_file;
while ( defined( my $line = <> ) )
{
chomp $line;
my ($quantity, $item) = split " ", $line, 2;
$sold{$item} = $quantity;
}
@ARGV = @saved_argv;
}
else
{
die "Usage: $0 -s sales_file inventory file...\n";
}
my (%item_cost, %inventory);
while ( defined( my $line = <> ) )
{
chomp $line;
my ($cost, $quantity, $item) = split ",", $line, 3;
$item_cost{$item} = $cost;
$inventory{$item} = $quantity;
}
foreach my $item ( keys %sold )
{
if ( exists $inventory{$item} )
{
$inventory{$item} -= $sold{$item};
}
else
{
warn "*** Sold $sold{$item} of $item, which were not in inventory\n";
}
}
foreach my $item ( sort keys %item_cost )
{
printf "%5.2f %6d %s\n", $item_cost{$item}, $inventory{$item}, $item;
}
Any help would be appreciated, I have looked long enough that I fear I am over thinking this....