Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Random Access Files in PERL

Posted on 1997-06-23
3
Medium Priority
?
178 Views
Last Modified: 2006-11-17
I want to be able to have random access files where I can read a particular record, or rewrite a particular record somewhat like I do in Visual Basic.  I have several Sams books on PERL but can find nothing about random access file handling.  Can anyone point me in the right direction or tell me how to do it?
I want to use it in a flat file database application.
0
Comment
Question by:donb1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1204342
seek() is what you need, and read() and print(); have a look at these functions.

-julio
0
 
LVL 5

Accepted Solution

by:
icd earned 300 total points
ID: 1204343
As an example, you can create a fixed length record file as follows. This example holds integers (four bytes ones) and allows random access to them.

--- cut here ---
# Create a file with four integer

$foo = pack("iiii", 0, 1, 2, 3);
open (FID, ">pack.dat");
print FID $foo;
close (FID);

# random access to record 2

$sizeofint = 4;

open (FID, "pack.dat");
seek(FID,2*$sizeofint,0);
read(FID, $buf, $sizeofint);
close(FID);

$foo = unpack("i", $buf);
print "The unpacked value is $foo\n";
--- end ---

0
 

Expert Comment

by:stevemanes
ID: 1204344
However, to retrieve a particular record from a file which was written by multiple pack()s you will need to know the length of the data record to know the offset to the beginning of record 'n'.  Since perl doesn't use structures like C, the quick and dirty way is to read the length() of the output from pack() and save it for future references.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question