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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get sum total of strings 1

Status
Not open for further replies.

GZPM

IS-IT--Management
Jan 20, 2006
18
US
I need help figuring out how to incorporate/get sum total of strings, while inside a foreach block. any help will be appreciated.

Here's the code:
_________________________________________
#!usr/bin/perl
use strict;
use File::stat;

our $dir_path = 'C:\path_name';
open DIR, $dir_path or die "Can't locate $dir_path: $!";
our @xml_files = glob '*.xml';
foreach my $xml_file_name (@xml_files) {
open FILE, $xml_file_name or die "Can't open $xml_file_name: $!";
my $xml_file_size = stat($xml_file_name)->size;
print "$xml_file_name is $xml_file_size BYTES ";

foreach my $line (<FILE>) {
if ($line =~ /^<product>/i) {
my @count = shift;
print "and contains ", scalar(@count), " record.\n";

}

}
}
close FILE;
close DIR;
=========================================
Here's what i'm getting right now:

Books_10292006_onix21.xml is 36670 BYTES and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
STER_10292006_bn_onix21.xml is 17393 BYTES and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
and contains 1 record.
==============================================
Here's the output i need from it:

Books_10292006_onix21.xml is 36670 BYTES and contains 13 record.
STER_10292006_bn_onix21.xml is 17393 BYTES and contains 6 record.
 
just change you embedded for loop a little.

################
my $count = 0;
foreach my $line (<FILE>) {
if ($line =~ /^<product>/i) {
$count++;
}
}
print "and contains " . $count . " records.\n";
################
 
Thanks garybroadwater, it worked like a charm!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top