Solved

Random Access Files in PERL

Posted on 1997-06-23
3
158 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
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 100 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl string replace for refred url 9 57
Migrate OTRS to newest version. 1 303
add a syntax to a csv file 8 92
Perl Frameworks 1 55
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now