×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Second file read from Diamond Operator need first line removed

Second file read from Diamond Operator need first line removed

Second file read from Diamond Operator need first line removed

(OP)
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....

RE: Second file read from Diamond Operator need first line removed

Hi

Quote (man perlvar)

$. Current line number for the last filehandle accessed.

So to skip line 1 :

CODE --> )

my (%item_cost, %inventory);
while ( defined( my $line = <> ) )
{
  next if $. == 1;

  chomp $line;

  my ($cost, $quantity, $item) = split ",", $line, 3;
  $item_cost{$item} = $cost;
  $inventory{$item} = $quantity;
} 

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

Feherke.
feherke.ga

RE: Second file read from Diamond Operator need first line removed

(OP)
Feherke,
thank you, told you, over thinking it. Sorry for not notating my post.
thanks again

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close