Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Second file read from Diamond Operator need first line removed

Status
Not open for further replies.

mac9968

IS-IT--Management
Apr 5, 2011
9
US
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....
 
Hi

man perlvar said:
[tt]$. Current line number for the last filehandle accessed.[/tt]

So to skip line 1 :
Code:
[b]my[/b] [teal]([/teal][navy]%item_cost[/navy][teal],[/teal] [navy]%inventory[/navy][teal]);[/teal]
[b]while[/b] [teal]([/teal] [b]defined[/b][teal]([/teal] [b]my[/b] [navy]$line[/navy] [teal]=[/teal] [i][COLOR=#009900]<>[/color][/i] [teal])[/teal] [teal])[/teal]
[teal]{[/teal]
  [highlight][b]next[/b] [b]if[/b] $[teal].[/teal] [teal]==[/teal] [purple]1[/purple][teal];[/teal][/highlight]

  [b]chomp[/b] [navy]$line[/navy][teal];[/teal]

  [b]my[/b] [teal]([/teal][navy]$cost[/navy][teal],[/teal] [navy]$quantity[/navy][teal],[/teal] [navy]$item[/navy][teal])[/teal] [teal]=[/teal] [b]split[/b] [i][green]","[/green][/i][teal],[/teal] [navy]$line[/navy][teal],[/teal] [purple]3[/purple][teal];[/teal]
  [navy]$item_cost[/navy][teal]{[/teal][navy]$item[/navy][teal]}[/teal] [teal]=[/teal] [navy]$cost[/navy][teal];[/teal]
  [navy]$inventory[/navy][teal]{[/teal][navy]$item[/navy][teal]}[/teal] [teal]=[/teal] [navy]$quantity[/navy][teal];[/teal]
[teal]}[/teal]

Next time please mark your code with [tt][ignore]
Code:
[/ignore][/tt]..[tt][ignore]
[/ignore][/tt] TGML tags to preserve indentation.

Feherke.
feherke.ga
 
Feherke,
thank you, told you, over thinking it. Sorry for not notating my post.
thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top