 | 
August 14th, 2008, 08:23 PM
| | Newbie | | Join Date: Aug 2008
Posts: 2
| | reading/writing records from/to a file
Hi,
I want to write a perl code that does these operations:
1- it gets data from keyboard and save it in a structure like this:
struct BookData
{
char bookTitle[51];
char isbn [14];
char author[31];
char publisher[31];
char dateAdded[11];
int qtyOnHand ;
float wholesaleprice ;
float retailprice ;
};
2- then it saves the data structure in a file. let's call it: "book.Dat"
3- it then retreives "book.Dat" to prepare some reports (number of books on hand, sorted list of book names ...)
how do I do this in perl? Thank you.
| 
August 14th, 2008, 08:58 PM
|  | Forum Leader | | Join Date: May 2007 Location: New Hampshire
Posts: 2,156
| |
First, even though you have worded this somewhat non-discretely, it sounds peculiarly like something they would ask you to do in school.
So, what have you tried thus far to do this? Do you have any code to show so far so we can see how far you have gotten with this?
I can think of a couple of ways to do this. One involves a file, as you mentioned, maybe a csv file, the other is to use a database instead of a file and incorporate DBI into the script. That way you can retrieve the information with code instead of having to open the file each time.
How much Perl do you know? Also, is that "struct" you showed in your posting from C? Just wondering if this was a conversion from C to Perl, that's all.
Also, when including code in the forums, please use code tags!
Regards,
Jeff
| 
August 14th, 2008, 09:31 PM
| | Newbie | | Join Date: Aug 2008
Posts: 2
| |
Hi Jeff,
Thanks for your reply. I know C/C++ at intermediate level and I just started learning perl. that is why I used struct example. in C, I define a struct( a structure), read data from keyboard to it and I save it in a file. then I can open the file, get any record I want and get my reports. for example: I sort the data based on booktitle, or quantity on hand, and so on.I want to do the same thing in perl.
what I have written so far is a perl program that gets Book information such as: Book title, ISBN, Author, quantity on hand and wholesale price. it then puts the information in a hash. then user can see a list of available books in data base.
if you run this program you see it is working fine, but when you uncomment those lines related to tie my hash to data base "book.dbm" you will see that program can not provide a list of records in database to user. do u know what the problem is or shall I use a totally different approach? Thank you.
here is the perl code: -
#!/usr/local/bin/perl
-
use warnings;
-
#use strict; # turn off for anonymous reference
-
use Data::Dumper;
-
use POSIX;
-
use SDBM_File; # or GDBM_File / NDBM_File / AnyDBM_File...
-
-
sub main_menu();
-
sub list();
-
sub add();
-
-
#my %addressbook;
-
#my $db_file = "bookinfo.dbm";
-
-
#tie %addressbook, 'SDBM_File', $db_file, O_CREAT|O_RDWR, 0644;
-
-
#if (tied %addressbook)
-
#{
-
# print "File $db_file now open.\n";
-
#}
-
#else
-
#{
-
# die "Sorry - unable to open $db_file\n";
-
#};
-
-
while (1)
-
{
-
main_menu();
-
print "\nPlease Enter Your Choice:";
-
chomp($_ = <STDIN>);
-
if ($_ eq "2") { list() }
-
elsif ($_ eq "3") { list() }
-
elsif ($_ eq "1") { add() }
-
elsif ($_ eq "4") { print "\nGood Bye!"; exit ; }
-
else { print "Sorry, not a recognized option.\n"; }
-
}
-
#untie %addressbook;
-
-
#******** sub add() *******************************
-
sub add()
-
{
-
print "\nPlease Enter Following information: \n\n";
-
print "\nBook Title:"; chomp( $title = <STDIN>);
-
print "\nISBN number:";chomp( $isbn = <STDIN>);
-
print "\nAuthor's Name:"; chomp( $author = <STDIN>);
-
print "\nThe quantity of this book being added:";chomp( $qty = <STDIN>);
-
print "\nThe wholesale cost of this book:"; chomp( $whole = <STDIN>);
-
-
$addressbook{$isbn} = {
-
title =>$title ,
-
author=>$author ,
-
qtyOnHand => $qty ,
-
wholesale => $whole
-
};
-
print "\nHere is AddressBook so far:" ;
-
print Dumper $addressbook{$isbn} ;
-
-
return;
-
}
-
-
#******** sub list *******************************
-
sub list()
-
{
-
#print Dumper \%addressbook ;
-
foreach (sort keys(%addressbook))
-
{
-
print "\nISBN: $_ ";
-
print "\nBook Title: ", $addressbook{$_}->{title};
-
print "\nAuthor: ", $addressbook{$_}->{author};
-
print "\nQuantity on Hand: ", $addressbook{$_}->{qtyOnHand};
-
print "\nWhole Sale Price: ", $addressbook{$_}->{wholesale},"\n";
-
}
-
return;
-
}
-
-
#******** sub main_menu *******************************
-
sub main_menu()
-
{
-
print<<EOF;
-
-
Bookinfo- Perl Version 2.0
-
-
Main Menu:
-
-
1- add record to data base
-
2- list records in data base
-
3- list records in data base
-
4- Exit
-
EOF
-
}
-
-
Quote: |
Originally Posted by numberwhun First, even though you have worded this somewhat non-discretely, it sounds peculiarly like something they would ask you to do in school.
So, what have you tried thus far to do this? Do you have any code to show so far so we can see how far you have gotten with this?
I can think of a couple of ways to do this. One involves a file, as you mentioned, maybe a csv file, the other is to use a database instead of a file and incorporate DBI into the script. That way you can retrieve the information with code instead of having to open the file each time.
How much Perl do you know? Also, is that "struct" you showed in your posting from C? Just wondering if this was a conversion from C to Perl, that's all.
Also, when including code in the forums, please use code tags!
Regards,
Jeff | |  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|