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!

reading and returning 2 files simultaneously 1

Status
Not open for further replies.

artisst

Programmer
Apr 24, 2004
18
US
I need to know how to read from 2 tab-delimited flat files with the while statement. The code I have below only reads from the first one, when I change it to SIDX2 it only reads from the second. I need both files to open and hold them in separate array lists.


Code:
open (SIDX, "$data_dir/search.idx");
open (SIDX2, "$data_dir/search2.idx");

 if ($file_locking ne "No"){flock (SIDX, LOCK_SH) or die "Can't set lock for file: $data_dir/search.idx, $data_dir/search2.idx $!\n";}
	while (defined($line=<SIDX>) and ($premiumline=<SIDX2>))
	{
	$sline = $line, $premiumline;
	
	foreach $kwr (@skeyw, @premiumkeyw)
		{

		if (($sline =~ /$kwr/i) and ($kwr ne ""))
			{
			$toadk = "true";
			}
			
		}

		if ($toadk eq "true")
			{
And here I need an additional result line like $premiumresultline = $premiumline without the $icnt counter

Code:
			$resultline[$icnt] = $line;
			$toadk = false;
			$icnt++;
			}

	}
 #if ($file_locking ne "No"){flock (CIT, LOCK_UN);}
close (SIDX);
close (SIDX2);

}
 
No, it's reading from both files. The value of $premiumline is being discarded when you say
$sline = $line, $premiumline;
This is not how you do concatenation. Take a look at the join() function and the . (dot) operator.

However, more importantly, you need to rethink your approach.
Reading the 2 files simultaneously, what happens when you reach the end of the shorter file? Consider reading the 2 files separately.
 
You can also first read the files into arrays and then manipulate the arrays. This enables you to free the files quicker. Afcourse only important if you working with multiple persons on the platform.
 
I am trying to open and search 2 flatdbs and putting the search word results from file #1 in one column and the same search word results from file #2 in another column on the same html page which uses %%placeholders%%
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top