Solved

Random Access Files in PERL

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script help 5 233
Filtering a file to table 9 99
rename outfile before writing 2 71
Put query string from URL request -  PERL HTTP Daemon 2 73
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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

22 Experts available now in Live!

Get 1:1 Help Now