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

Building an arrary

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I am trying to build a 3 element arry that has no duplicates. I am getting a one element array.

Can somebody help me?


open(IN, "D:\\EDI\\810\\ma540csv") or die 'Could not open file for reading';

foreach $line (<IN>)
{
@field = split(/\,/, $line);
$numb = substr($field[10],0,1); # first digit of number
if ($field[0] eq "H")
{
$f_index = 0;
$comp = $field[1];
$vend = $field[2]);
for ($i= 0; $i < @array; $i++)
{
if ($array[$i] eq $comb)
{
$f_index = $i;
last;
}
}
if ($f_index < @array)
{
$count[$f_index]++; # found & $f_index is index
}
else
{
$count = 1;
@list = ($comp,$vend,$count); # not found so add to array
push(@array, @list); # add element to array
}
}
}
close(IN);

The data is

H,0000, 514659,GHXPCCONNECT810,45066599,,,,,,1533864
D,0000, 514659,GHXPCCONNECT810,45066599,,1,,,,1533864
H,0000, 514659,GHXPCCONNECT810,45066599,,,,,,7533864
D,0000, 514659,GHXPCCONNECT810,45066599,,1,,,,7533864
H,0000, 514659,GHXPCCONNECT810,45066599,,,,,,8533864
D,0000, 514659,GHXPCCONNECT810,45066599,,1,,,,8533864
H,0000, 514659,GHXPCCONNECT810,45066599,,,,,,1533864
D,0000, 514659,GHXPCCONNECT810,45066599,,1,,,,1533864


 
The array needs to hold the company, vendor, and the number of times the company/vendor combination was in the list.
 
jcarrott said:
The data is

H,0000, 514659,GHXPCCONNECT810,45066599,,,,,,1533864
Which field is the company and which is the vendor?
 
I am sorry I forgot to run the first part of the program on the test data.

Here is the correct test data

H,0120, 514659,GHXPCCONNECT810,45066599,,,,,,1533864
D,0000, 514659,GHXPCCONNECT810,45066599,,1,,,,1533864
H,0710, 514659,GHXPCCONNECT810,45066599,,,,,,7533864
D,0000, 514659,GHXPCCONNECT810,45066599,,1,,,,7533864
H,0800, 514659,GHXPCCONNECT810,45066599,,,,,,8533864
D,0000, 514659,GHXPCCONNECT810,45066599,,1,,,,8533864
H,0120, 514659,GHXPCCONNECT810,45066599,,,,,,1533864
D,0000, 514659,GHXPCCONNECT810,45066599,,1,,,,1533864

column 1 is type
column 2 is company
column 3 is vendor

For this data there are 3 companies and 1 vendor. One company is in the list twice.
 
Also, the vendor number has 3 leading spaces and I need to keep those. Vendor is use in the system as right justified with leading spaces.
 
When I run the code I get
$array[0]: 0120
$array[1]: 514659
$array[2]: 1
$array[3]: 0710
$array[4]: 514659
$array[5]: 1
$array[6]: 0800
$array[7]: 514659
$array[8]: 1
$array[9]: 0120
$array[10]: 514659
$array[11]: 1

It looks like I need an array of arrays to have the 3 items in one row.
 
I found a working solution

open(IN, "D:\\EDI\\810\\ma540csv") or die 'Could not open file for reading';

$arow = 0;

foreach $line (<IN>)
{
@field = split(/\,/, $line);
$numb = substr($field[10],0,1); # first digit of number
if ($field[0] eq "H")
{
$f_index = 0;
$comp = $field[1];
$vend = $field[2];
$comb = $comp . "-" . $vend;
for ($i= 0; $i < @array; $i++)
{
if ($array[$i][0] eq $comb)
{
$f_index = $i;
last;
}
}
if ($i < @array)
{
$array[$i][1]++; # found & $f_index is index
}
else
{
$count = 1;
@array[$arow] = ([$comb,$count]); # not found so add to array
$arow++;
}
}
}
close(IN);
 
Hmm.. you should really look into usin hashes.

This might be a little easier?
Code:
my (@order, %seen);
while (<DATA>) {
	my ($type, $company, $vendor, @rest) = split(',', $_);
	unless ($seen{"$company-$vendor"}++) {
		push @order, "$company-$vendor";
	}	
}

foreach (@order) {
	print "$_ : $seen{$_}\n";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top