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!

read data file

Status
Not open for further replies.

lmb5kids

Programmer
Dec 18, 2003
36
US
I need some help getting started on how to read and format a data file. The data file contains names listed at top of file followed by products ,product numbers and UPC. The file layout is listed below :
Name1
Name2
Name3
ProductName1
ProductNumber1
ProductCode1(links to Name1)
ProductCode2(links to Name2)
ProductCode3(links to Name3)
ProductName2
ProductNumber2
ProductCode1(links to Name1)
ProductCode2(links to Name2)
ProductCode3(links to Name3)
and so forth ...
 
you read files generally using the open() function.

Code:
open(FILE,'path/to/file.dat') or die "$!";
while(<FILE>) {
   do something
}
close(FILE);

How do you want to "format" the file?
 
I would like to give the program an arguement like this :
program.pl -nName1 -pProductName1 and it would print out something like this :
Name1 ProductName1 ProductNumber1 ProductCode1

Thanks for your help
 
Looks like you have essentially relational data in a flat file. While you could create a structure based on hashes and arrays, it might be complex and inflexible. Suggest you use perl to read the data from the file, and load some SQL tables, maybe something like:
Code:
+-----------+    +-------------+    +-------------+
|Names      |    | NameProduct |    | Product     |
+-----------+    +-------------+    +-------------+
|NameId     |<---|NameId       |    |ProductId    |
|Name       |    |ProductId    |--->|ProductName  |
+-----------+    |Code         |    +-------------+
                 +-------------+
Once your data are in this form, it's fairly trivial to use SQL to get what you want.
Code:
# untested!
SELECT
   Name, ProductName, Product.ProductId, Code
   FROM Names
   INNER JOIN NameProduct
   ON Names.Nameid = NameProduct.NameId
   INNER JOIN Product
   ON NameProduct.ProductId = Product.ProductId
   WHERE Name = ? AND ProductName = ?;
This also gives you the flexibility to run any other kinds of 'what-if' query supported by the database, like counting, averaging, etc.

I know this isn't strictly a perl solution, but perl has excellent support for databases via the DBI module.
 
thanks for the reply. I still need help on the perl program.How do I associate the Name? with the product code ?
 
You need to use an associative array lmb5kids, have a look in the documentation.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
thanks for the tip , I am still a little confused on this though. The first 30 lines of the file are going to be the Name1-Name30 and they are not going to be in the same order all the time, so I would I go about that?
 
is it possible to restructure the data file? Why can't you use a more logical/efficient format?

Code:
Name1,ProductName1,ProductNumber1,ProductCode1(links to Name1)
Name2,ProductName2,ProductNumber2,ProductCode2(links to Name2)
Name3,ProductName3,ProductNumber3,ProductCode3(links to Name3)

this way each line of the file has all the related data together and you can just split the lines into seperate fields.

Code:
while(<FILE>) {
   chomp;
   my ($name,$p_name,$p_num,$p_code) = split(/,/);
   do somthing with the data
}

or use one of the CSV modules to work with the data.
 
unfortunately there is no way to reformat the data , this is how the file is currently layed out.
 
unfortunately there is no way to reformat the data

Yes there is.... perl! :)

that being said, post some real lines from the file. It looks like what you posted are generic examples of data, but the solution is going to require non-generic data to try some code on.

 
ok here is an example of the file :

MIKE
JOHN
SALLY
SUE
DUDE
BONNY
BOB
LORI
MIKE
JIM
JAMES
BO
TOM
BETH
JON
CHRIS
GREG
SAL
TIM
LARRY
-2
HARRY
KIP
POP
DON
COLT
RON
-2
-2
-2
-2
-2
MOW <----- 1st product starts here
MOWER BLADE
23
MOW
221
*

*
*
9
*
*
3
*
*
03
*
03



01
*
*
*
*
*
028
*
*
*
*
*
*
and then the next product starts here
 
forgot to say that the -2 are considered blank or open records for names
 
wow... I have no idea what you could do with that file. Maybe it's just friday and my brain needs some R&R (or a few margaritas).
 
thanks, hopefully after the weekend someone can shed some light on this for me.
 
is each section of data a fixed number of lines? Without any other way to divide the file into "chunks" I can't see how you could know where one part of the data begins or ends. [ponder]
 
each section has a set number of lines :
first 32 lines are names
then each section after that is 34 lines:
first 2 lines are product name and product number
next 32 lines are codes that correspond to the 32 names listed first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top