Creating a vlookup style code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattiep
Forum Newbie
Posts: 7
Joined: Wed Feb 09, 2005 4:09 am
Location: London, UK

Creating a vlookup style code

Post by mattiep »

Hi,

I have 2 TAB delimited text files, both have around 1000 lines, they come from different sources, but both have a common identifier.

What I want to do is merge these two files together. And save them as CSV or TAB delimited (it doesnt matter). In one of the lines of the data I will need to have all 5 fields from the first text file, followed by one of the fields from the second text file. I will then need to fwrite() this to another file.

TIA.

Mattie P
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

and where is your problem?

reading up on http://www.php.net/fgetcsv and related stuff should get you going...
mattiep
Forum Newbie
Posts: 7
Joined: Wed Feb 09, 2005 4:09 am
Location: London, UK

Post by mattiep »

I can read the first file and export that into an output file, using the following:

$fgv = fopen($mylistfile, "r");
$vlinecounter=0;
while($vline = fgets($fgv,4096)) {
if($vlinecounter++==0) {
continue;
}
list($A,$B,$C) = split(" ", $vline);

But I am unable to read the second file & combine using this method.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

you can simply use file() to read in contents of file as array thereafter do as you wish with the contents. selct, copy, write somewhere else etc.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

in each loop you should not only rread from file1, but also from file2..
can't see the difficulty
mattiep
Forum Newbie
Posts: 7
Joined: Wed Feb 09, 2005 4:09 am
Location: London, UK

Post by mattiep »

How do I read the second file in the same loop? I tried to create a second loop under the first loop, but this caused the page to crash.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sounds like you second loop was either an infinite loop, or an exponential i.e. inside the original loop.
mattiep
Forum Newbie
Posts: 7
Joined: Wed Feb 09, 2005 4:09 am
Location: London, UK

Post by mattiep »

OK, I am pretty new to PHP, so all of this is probably very helpful, but could you simplify it for me??

Ta
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post your new code that "crashes"
mattiep
Forum Newbie
Posts: 7
Joined: Wed Feb 09, 2005 4:09 am
Location: London, UK

Post by mattiep »

Hi Feyd,

Here is the code:

<?php
$fgv = fopen($mylistfile, "r");
$vlinecounter=0;
while($vline = fgets($fgv,4096)) {
if($vlinecounter++==0) {
continue;
}
list($CaID,$CeID,$CHwR,$CN,$DD,$A,$Gen,$D,$P,$SA,$TC,$OC,$FD,$FN,$RT,$AT,$IH,$LN,$DE,$St,$Geo,$BR,$M,$SLA,$EOL) = split(" ", $vline);

$BSCPA='';
$fgc=fopen($myotherfile, "r");
$clinecounter=0;
while($cline = fgets($fgc,4096)) {
if($clinecounter++==0) {
continue;
}
list($bpaCSR,$bpaSN,$bpaSSF1,$bpaSSF19,$bpaSSF20,$bpaBSCid,$bpaBSCPA) = split(" ", $cline);
if($bpaCSR==$CeID) { $BSCPA=$bpaBSCPA; }

}



fwrite($fwop,$CaID.' '.$CeID.' '.$CHwR.' '.$CN.' '.$DD.' '.$A.' '.$Gen.' '.$D.' '.$P.' '.$SA.' '.$TC.' '.$OC.' '.$FD.' '.$FN.' '.$RT.' '.$AT.' '.$IH.' '.$LN.' '.$DE.' '.$St.' '.$Geo.' '.$BR.' '.$M.' '.$SLA.' '.$BSCPA."\n");

}


fclose($fgc);
fclose($fgv);
fclose($fwop);
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

good god! that's sure to crash and screw your system.
that's crashin' because you are opening second file inside first loop when first file is also open and not closing it inside first loop itself. You are closing it outside of the loop. Just close it inside of the first loop.
and second thing is this

Code: Select all

list($bpaCSR,$bpaSN,$bpaSSF1,$bpaSSF19,$bpaSSF20,$bpaBSCid,$bpaBSCPA) = split(" ", $cline); 
if($bpaCSR==$CeID) &#123; $BSCPA=$bpaBSCPA; &#125;
can be shortened to this

Code: Select all

list($bpaCSR,,,,,,$bpaBSCPA) = split(" ", $cline); 
if($bpaCSR==$CeID) &#123; $BSCPA=$bpaBSCPA; &#125;
PS: When Posting code in Forum please use code tags
mattiep
Forum Newbie
Posts: 7
Joined: Wed Feb 09, 2005 4:09 am
Location: London, UK

Post by mattiep »

Where abouts in the loop should I close the file? I closed it after the list... part, and the script errored
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

just put it before this line but after the curly brace of while

Code: Select all

fwrite($fwop,$CaID.' '.$CeID.' '.$CHwR.' '.$CN ...
Post Reply