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

Compare HASH table with Array return new Array with changed Value

Status
Not open for further replies.

kb0rhe

Technical User
Dec 25, 2006
16
US
What I need to do is to export a CSV file (see "FILE2 Example") and change the price of that part e.g. "2.64" to "3.28.
I have a list of 4000 parts that need to be compared with a file that might have 20K+ lines (New prices for parts).

What I was trying to do is create a "Hash" of FILE1 consisting of the part number and its corresponding price.
Then in FILE2 search using the part number and aligning the part number in the hash to return the new price.
Put the price back into the 'string' and write all of that to a new file.

My main Question would be how can I do this HASH compare?
Or How to I execute a 'sub' inside the second while statement.


FILE1 Example
AYP :104757X421 : 3.28 "HUB CAP AXLE"

FILE2 Example
,Active,Inventory Part,AYP-A2:104757X421,HUB CAP AXLE,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,HUB CAP AXLE,1.00,0.99,,,2.64,

FILE 2 Example is one line of data with that consists of 17 positions in an array.

"CODE START"
use strict;

open (FILE1, "C:\\CNTWork\\items1.csv") or die "Sorry!";
open (FILE2, "C:\\CNTWork\\output1.txt") or die "Sorry Wont open!";

while (<FILE2>) #OPEN THE FILE TO GET A PART NUMBER => PRICE.
{
my $line = $_;
my @a = split(/ /,$line);
my %hash;
%hash = $a[1]." ". $a[2]. "\n";
}
close (FILE2); #THIS SHOULD CREATE A HASH CONSISTING OF A PART NUMBER and its cooresponding price

while (<FILE1>) #THis file will open the CSV file replace the cost @array[13] with the value found in %hash.
{
my @array = split(/,/,<FILE1>);
#my $str1 = $array[15];
#print $array[3]. " ";
#print $array[15] . "\n";
print $array[0] .",". $array[1].",". $array[2].",".$array[3].",". $array[4].",". $array[5].",".$array[6].",".$array[7].",".$array[8].",".$array[9].",".$array[10].",".$array[11].",".$array[12].",".$array[13].",".$array[14].",".$array[15].",".$array[16].",".$array[17];
print $array[3]."\n";
print $hash ; #ERROR HERE, I wanted to see if the hash has a value
}
close (FILE1)
**** CODE END ****

The second "while" is looping though a file that has parts and prices (to make a long story short) the purpose of the hash is to have a reference of the part number to a new price.

I am really new to this. I have been tryhing to think thorough ways of doing this, but I keep coming up short.
 
hmm... sorry to say your code is well off the mark. You are thinking along the right lines but the code is just not right.

Explain this line of file1 better:

AYP :104757X421 : 3.28 "HUB CAP AXLE"

what in that line constitutes the part number? Is there some consistent format in that line that is the same in all the rest of the lines in that file?




- Kevin, perl coder unexceptional!
 
making some assumptions, this might correctly edit the file inplace. Test with backup copies of your files, make corrections as needed or post for more help.

Code:
my %hash;
open(IN, 'C:/CNTWork/output1.txt') or die "Can't open output1.txt: $!";
while (<IN>) {
   my ($n,$p) = $_ =~ /^(\S+\s*:\S+)\s*:\s*(\S+)/;
   $hash{$n} = $p;
}
close(IN);

{  
   local @ARGV = ('C:/CNTWork/items1.csv');
   local $^I = '.bac';
   while(<>) {
      chomp;
      my @array = split(/,/);
      $array[13] = (exists $hash{$array[2]}) ? $hash{$array[2]} : $array[13];
      print join(',',@array),"\n";
   }
}

- Kevin, perl coder unexceptional!
 
Hey I have been working with Perl for a few weeks now....

The portion between the colon's ":" Is the part number.

AYP :104757X421:

:104757X421:

Thanks for your response! I do appreciate it.

I can't wait to try your code. -D
 
I get this error:

Use of uninitialized value in join or string at C:\Program Files\LuckaSoft\EngInSite Perl Editor\Hash.pl line 17, <> line 4304.

What is wrong.. please

{
local @ARGV = ('C:/CNTWork/items1.csv');
local $^I = '.bac';
while(<>) {
chomp;
my @array = split(/,/);
$array[13] = (exists $hash{$array[2]}) ? $hash{$array[2]} : $array[13];
print join(',',@array),"\n";

Line 17 is " print join(',',@array),"\n";
 
I don't have enough information to know where the problem is. I worked up my code based on only the two sample lines you posted. Your real data could be different. You said the part number is between the colons:

AYP :104757X421 : 3.28 "HUB CAP AXLE"

so the part number is 104757X421

but in the other line you posted the part number is formatted like this:

AYP-A2:104757X421

So need to see more data or you need to explain things in more detail.



- Kevin, perl coder unexceptional!
 
Kevin,
The main problem is this. I will try to explain:
I have to change values in "FILE 2" based on what is found in "FILE 1"

FILE 1 contains a part number and price.

FILE 2 Needs to have the price updated based on what is found in file1.

FILE 1 simplified (Might actually contain 10K lines):
{part-number}{space}{price}
X123 2.12
X234 3.12

FILE 2 simplified (will contain 4000 Lines);
{part-Number}{X-X= Columns}{price}{}
AYP-1:X123 X,X,X,X,X,X,1.12,X,X
AYP-2:X999 X,X,X,X,X,X,9.99,X,X
AYP-1:X234 X,X,X,X,X,X,0.12,X,X

So what I need to do for each line in File2: I need to compare with file 1 and change the price (column 8)in file 2.

So what I was wondering is to load FILE 1 as a hash...
Loop through FILE2 a line at a time, compare to 'hash' and get the following Result.
AYP-1:X123 X,X,X,X,X,X,2.12,X,X
AYP-2:X999 X,X,X,X,X,X,9.99,X,X
AYP-1:X234 X,X,X,X,X,X,3.12,X,X

X123 is the part number.

I hope this is a better explaination of my issue.
Heres the deal. I believe I can get the comparison of part number I don't know how to compare and print the new price.

-Thanks again for your help
Douglas
 
The code I posted above will work. All that needs to be ironed out is the part numbers. Post some real lines of data from both files.

- Kevin, perl coder unexceptional!
 
FILE 1:
BRG :262500: 3.55 #SPRING-GOVERNOR LINK #
BRG :262501: 0.00 #Spring-Valve #
BRG :262504: 4.60 #SPRING-GOVERNED IDLE #
BRG :262505: 0.00 #SPRING, GOVERNOR #
BRG :262506: 3.75 #SPRING-CAM RELEASE #
BRG :262507: 0.00 #CRANKSHAFT #
BRG :262514: 2.60 #LOCK-PISTON PIN #
BRG :262515: 25.70 #GEAR-TIMING #
BRG :262518: 3.50 #LINK-CHOKE #
BRG :262521: 4.00 #TAPPET-VALVE #
BRG :262529: 37.40 #KIT-BUSHING/SEAL #
BRG :262530: 4.40 #SPRING-GOVERNOR LINK #
BRG :262532: 16.60 #SEAT-VALVE #
BRG :262534: 4.00 #SEAT-VALVE #
BRG :262535: 7.45 #SEAT-VALVE #
BRG :262536: 13.55 #LINK-CONTROL #
BRG :262537: 4.25 #LINK-CHOKE #
BRG :262538: 7.65 #LINK-CHOKE #
BRG :262539: 2.35 #SPRING-THR RETURN #
BRG :262542: 0.00 #CRANKSHAFT #
BRG :262546: 0.00 #CRANKSHAFT #
BRG :262549: 0.00 #Rod-Governor Control #

FILE 2:
,Active,Inventory Part,BSA-10:262500,LINK SPRING,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,LINK SPRING,2.00,1.80,,,3.40,1.00
,Active,Inventory Part,BSA-10:262651S,EXHAUST VALVE,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,EXHAUST VALVE,1.00,6.42,,,11.70
,Active,Inventory Part,BSA-10:262652S,INTAKE VALVE,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,INTAKE VALVE,2.00,4.74,,,8.90
,Active,Inventory Part,BSA-10:262760,GOVERNOR SPRING,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,GOVERNOR SPRING,5.00,2.04,,,4.40
,Active,Inventory Part,BSA-10:270073,TANK GASKET,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,TANK GASKET,5.00,0.96,,,2.60,1.00
,Active,Inventory Part,BSA-10:270253,DIAPHRAGM,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,DIAPHRAGM,11.00,1.17,,,2.95,2.00
,Active,Inventory Part,BSA-10:270340,HEAD GASKET,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,HEAD GASKET,8.00,6.64,,,9.85,2.00
,Active,Inventory Part,BSA-10:270344,SEAT,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,SEAT,3.00,1.17,,,2.95,2.00
,Active,Inventory Part,BSA-10:270345,INTAKE GASKET,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,INTAKE GASKET,15.00,0.99,,,2.65,2.00
,Active,Inventory Part,BSA-10:270511,BOWL GASKET,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,BOWL GASKET,65.00,1.17,,,2.95,10.00
,Active,Inventory Part,BSA-10:270571,GASKET,Tax,4050 · Sales,5000 · Cost of Goods Sold,1300 · Inventory:1310 · Parts,0.00,GASKET,2.00,0.96,,,2.60
 
Code:
my %hash;
open(IN, 'output1.txt') or die "Can't open output1.txt: $!";
while (<IN>) {
   my ($n,$p) = $_ =~ /:([^:]+):\s*(\S+)/;
   $hash{$n} = $p;
}
close(IN);

{  
   local @ARGV = ('items1.csv');
   local $^I = '.bac';
   while(<>) {
      chomp;
      my @array = split(/,/);
      my ($n) = $array[3] =~ /:(\S+)/;
      $array[15] = (exists $hash{$n}) ? $hash{$n} : $array[15];
      print join(',',@array),"\n";
   }
}

- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top