"John W. Krahn" wrote:
> Adam Jimerson wrote:
>> I am working on a program that will act as a contact manager of sorts,
>> it uses two hashes to store everything (although the second one is
>> commented
>> out because I am not ready to work with it). I am using the format
>> function to display everything but I don't know how to get it to show the
>> keys to the
>> hash which is where the names are. Here is my code so far to show what I
>> am talking about:
>>
>> ,----[ CODE ]
>> #!/usr/bin/perl
>> use warnings;
>> use strict;
>>
>> #define hashes
>> my %phone_numbers = ();
>> #my %email_address = ();
>> my $full_name;
>> my $phone_number;
>>
>> print "CSCC Workstudy/Co-Op/Intern Contact Manager\n";
>> print "Version 0.6\n";
>>
>> if ("@ARGV" eq 'add') {
>
> That should be:
>
> if ($ARGV[0] eq 'add') {
>
>
>> dbmopen(%phone_numbers, "$ENV{HOME}/phone_numbers", 0766) || die "Can't
>> open database: $!\n"; #Open database for writing
>
> perldoc -f dbmopen
> dbmopen HASH,DBNAME,MASK
> [This function has been largely superseded by the "tie"
> function.]
>
> perldoc -f tie
> perldoc AnyDBM_File
> perldoc DB_File
> perldoc GDBM_File
> perldoc NDBM_File
> perldoc ODBM_File
> perldoc SDBM_File
>
>
>> print "\nFull Name: ";
>> chomp($full_name = <STDIN>);
>> print "Phone Number: ";
>> chomp($phone_number = <STDIN>);
>> $phone_numbers { $full_name } = $phone_number;
>> dbmclose(%phone_numbers) || die "Can't close database $!\n"; #saves and
>> closes the database
>> exit 0;
>> } elsif ("@ARGV" eq 'view') {
>> dbmopen(%phone_numbers, "$ENV{HOME}/phone_numbers", 0666) || die "Can't
>> open database: $!\n"; #Open database for reading
>> foreach (keys %phone_numbers) {
>> write;
>> }
>> exit 0;
>> } else {
>> die "Usage: $0 [add|view]\n";
>> }
>>
>> format STDOUT =
>> @<<<<<<<<<<<<<<< @##-###-####
>
> @##-###-#### is not a valid format string, that is a three digit number
> followed by the literal string '-###-####'.
>
>
>> $phone_numbers{$_} #needs to print the keys of the hash for the name, and
>> values for the phone number so it will be like this "Some Name
>> 555-555-5555" .
>>
>> format STDOUT_TOP =
>> Full Name Phone Numbers
>> ========= =============
>> .
>
> You probably want something more like this:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use DB_File;
>
> print "CSCC Workstudy/Co-Op/Intern Contact Manager\n",
> "Version 0.6\n";
>
> @ARGV == 1 or die "Usage: $0 [add|view]\n";
>
> # define hashes
> tie my %phone_numbers, 'DB_File', "$ENV{HOME}/phone_numbers"
> or die "Can't open $ENV{HOME}/phone_numbers: $!\n";
> #tie my %email_address, 'DB_File', "$ENV{HOME}/email_address"
> # or die "Can't open $ENV{HOME}/email_address: $!\n";
>
> if ( $ARGV[ 0 ] eq 'add' ) {
> print "\nFull Name: ";
> chomp( my $full_name = <STDIN> );
> print "Phone Number: ";
> chomp( my $phone_number = <STDIN> );
> $phone_numbers{ $full_name } = join '-', $phone_number =~
> /(\d{3})\D*(\d{3})\D*(\d{4})$/;
> }
> elsif ( $ARGV[ 0 ] eq 'view' ) {
> for ( keys %phone_numbers ) {
> write;
> }
> }
>
> format STDOUT_TOP =
> Full Name Phone Numbers
> ========= =============
> .
>
> format STDOUT =
> @<<<<<<<<<<<<<<< @>>>>>>>>>>>
> $_, $phone_numbers{$_}
> .
>
> untie %phone_numbers;
> exit 0;
>
> __END__
>
>
>
> John
Yea I read that dbmopen was superseded by the "tie" function but I couldn't
figure out the permissions for it:
tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
Which is what I though the 1,0 was there for. A quick question though, any
certain reason why you used DB_File in your code over any of the other
databases?
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/