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!

Working with two files 1

Status
Not open for further replies.

gm199

Programmer
Aug 9, 2001
37
US
Sorry guys to ask this, I'm lost. I searched previous post but find no help.
I have the following 2 files:
File 1 = ID#|Category (Pipe separated values, about different 1,300 lines)
File 2 = Url|Category|Link (30,000 lines)

What I need is to do is open File 2, split and find the ID# for the Category inside File 1 and write to new File 3 Url|ID#|Link
OK, I open File 1 and create an array then close, I open File 2 and "foreach" line I split using |

I dont know how to split each element in array from File 1 that is in memory already and compare. What is the correct sequence to open and close each file and how to split each element since each element is separated by | in array1?
 
What have you tried so far ?

You can open more than one file at a time for reading and/or writing, in Perl.

To split a line in a foreach loop try something like:

Code:
   foreach (@input_array) {
     @output_array = split /\|/, $_;
     print "@output_array\n";
   }

The correct sequence for using files:
open a file just before you want to use it (or at the start of the script)
read from or write to the file - depending on how it is opened
close the file when you have finished with it (or at the end of the script)

I hope that helps.

Mike
 
Thank you for anwering. Here what I have so far:
Code:
# All info is in cats.txt
#Have to open links.txt and for each line find the correspondig ID for the Cat name.

open(CATS, "cats.txt"); #format ID|Cat name
@catz=<CATS>;
close(CATS);

open(LINKS, "links.txt"); #format Url|Cat name|Link
@linkz=<LINKS>;

   foreach $new(@linkz){ 
     ($url, $catn,$link) = split(/\|/, $new);
	if($catn == Cat name from cats.txt;){ #<-- here is my problem. How to compare
                                                   with element in @catz since each
                                                   element is $ID|Cat?
          print "$Url | $ID | $Link\n";
        } #end if
     } #end foreach

close(LINKS);

Any help?
 
Use a hash to do the lookup to convert the category into the id. This is way more efficient. I've used a ternary operator in the print statement to hndle failed lookups, which might not be what you actually want. Coded from the hip I'm afraid, no perl here to test it with...
Code:
use strict;
use warnings;

my %categories;

open(FILE1, "file1.txt") or die $!;

while (<FILE1>) {
   my($id, $cat, undef) = split(/|/, $_);
   $categories{$cat} = $id;
}

close(FILE1);

open(FILE2, "file2.txt") or die $!;

while (<FILE2>) {
   my ($url, $cat, $link) = split(/|/, $_);
   print $url, "|",
      exists $categories{$cat} ? $categories{$cat} : "unknown",
      "|", $link, "\n";
}

close(FILE2);

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Probably want to escape the pipe in the split statements.../\|/

[blush]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hi Steve, Tks. Sintatically is correct, I got no errors.
BUT I always get the unknown as the category # even if the cat nme is the very same in both files.
Sorry for been so dumb but I didn't got exactly how the compare is made.
 
Borrowing heavily from steve's code:

Code:
use strict;
use warnings;

my %categories = ();

open(FILE1, "file1.txt") or die "$!";
while (<FILE1>) {
   chomp;
   my($id, $cat) = split(/\|/, $_);
   $categories{$cat} = $id;
}

close(FILE1);

open(FILE2, "file2.txt") or die "$!";
open(FILE3, ">file3.txt") or die "$!"; 
while (<FILE2>) {
   chomp;
   my ($url, $cat, $link) = split(/\|/, $_);
   if (exists $categories{$cat}) {
      print FILE3 "$url|$categories{$cat}|$link\n";
   }
}
close(FILE3);
close(FILE2);

see how that works.



- Kevin, perl coder unexceptional!
 
[medal] BINGO!!!

Thank you all of you for the help.
Have a very nice day as the one I will now after this problem solved!
[2thumbsup]
 
very good, credit to steve-->[reading], he did all the real work.

[atom]

- Kevin, perl coder unexceptional!
 
Yes, apart from the fact that it didn't work...

Must have a look at the ternary in the print statement - it's bound to be something stupid.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Yes, Kevin, like a building. One put the foundation, another the walls, someone the roof...
Again thank you all for the time spent, I was in a urgent need.
 
I know this doesn't help but I am interested in this same solution.

I have
X,PRTNO, X,X,X,123
In file 1

File two has

PRTNO, 234
Where 234 is updating file 1.

So a person should get

X, PRTNO, X,X,X,234...

 
kb0rhe

start a new thread for your question and try and explain what it is you need to do in more detail. Saying: "Where 234 is updating file 1" means pretty much nothing. Post any code you have tried so far.



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

Part and Inventory Search

Sponsor

Back
Top