reading lines from one file and pulling matching lines in another 
Author Message
 reading lines from one file and pulling matching lines in another

Hello,

I really have been working on this all morning and I'm stumped.  Here's
what I've got that I understand:

#!c:\perl\bin -w

        open (ZEROUNIQ, "zerouniq.txt");

        open (JANZERO, "janzero.sel");

zerouniq.txt is a list of id numbers, 1 per line.  janzero.sel is a list of
data records.  In each record is an id number.  I'd like to read the
id numbers listed in zerouniq.txt and pull only those lines that contain
the appropriate id numbers in janzero.sel.

I've looked in the cookbook but I can't seem to peice everything
together.  Can I do this with some sort of while (<JANZERO>)
construct?  Do I need to run through the arrays until I find members
that match?  

So, obviously I'm not a programmer by trade.  Any help is
greatly appreciated!

Thanks,

Eric



Sat, 03 Aug 2002 03:00:00 GMT  
 reading lines from one file and pulling matching lines in another


Quote:
> I really have been working on this all morning and I'm stumped.  Here's
> what I've got that I understand:

> #!c:\perl\bin -w

Good start.  Now add 'use strict;'

Quote:
>         open (ZEROUNIQ, "zerouniq.txt");

          open ZEROUNIQ, 'zerouniq.txt' or
              die "Couldn't open 'zerouniq.txt'. $!\n";


Put these into a hash.

          my %uniq;

Quote:
>         open (JANZERO, "janzero.sel");

Similarly -- diagnose failure to open.

Quote:

> zerouniq.txt is a list of id numbers, 1 per line.  janzero.sel is a list of
> data records.  In each record is an id number.  I'd like to read the
> id numbers listed in zerouniq.txt and pull only those lines that contain
> the appropriate id numbers in janzero.sel.

> I've looked in the cookbook but I can't seem to peice everything
> together.  Can I do this with some sort of while (<JANZERO>)
> construct?  Do I need to run through the arrays until I find members
> that match?  

> So, obviously I'm not a programmer by trade.  Any help is
> greatly appreciated!

The FAQ is there to help you, though if you are not a programmer maybe
you need more help than that.

perlfaq4: "How can I tell whether a list or array contains a certain
element?"

For each line in JANZERO, extract the number and see if it exists in the
hash.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sat, 03 Aug 2002 03:00:00 GMT  
 reading lines from one file and pulling matching lines in another
On Tue, 15 Feb 2000 17:30:05 GMT,

Quote:
> zerouniq.txt is a list of id numbers, 1 per line.  janzero.sel is a list of
> data records.  In each record is an id number.  I'd like to read the
> id numbers listed in zerouniq.txt and pull only those lines that contain
> the appropriate id numbers in janzero.sel.

Apart from Larry's comments:

It sounds like you're trying to do database like things with text
files. If the case stays as simple as this one, then the code to deal
with that isn't going to be too hard to write. If you start doing more
complex things you might want to use DBI with a text file based driver
(like DBD::CSV) for that. If you then ever decide that your data needs
to be moved to a real database, your code will need hardly any
changes.

http://www.symbolstone.org/technology/perl/DBI/

You can find more DBI drivers on www.cpan.org (they all start with
DBD::)

Martien
--
Martien Verbruggen              |
Interactive Media Division      | 42.6% of statistics is made up on the
Commercial Dynamics Pty. Ltd.   | spot.
NSW, Australia                  |



Sat, 03 Aug 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. search for one line and print that line and the one after it

2. combining every 3 lines in a given file into one line

3. combining every 3 lines in a given file into one line

4. cobol line seq. file - one very long line -

5. Pattern matching spanning more than one line and substitution between two files -

6. How to read one line from a text file

7. Read one line of a file

8. Reading a file as one line

9. How do I read line by line of data from a file using perl

10. how to read a huge file line by line without loading it into memory

11. newbie perl question - reading a file line by line

12. Line by line file reading

 

 
Powered by phpBB® Forum Software