Foncé has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing an address book (simple; just names and email addresses) both for my amusement and to learn more Perl. I've fought with this for a few hours and decided to seek professional help: I'm able to enter new entries, as the code will show, but I can't figure out how to remove entries from the outside file, called addybook.adb. What's the simplest, but reasonably effective way to do this? The code is as follows:
Thanks in advance!#!/usr/bin/perl -w use strict; # Declare yon variables my $ent; my $line; my @entry; my @lname; my @fname; my @email; my $addfname; my $addlname; my $addemail; my $addstring; my @addtemp; my $t_entry; my $option; my $x; $x = 1; while ($x == 1) { system "cls"; print "\nAddress Book\n------------\n"; print "Options:\nL for list\nA for add entry\nR for remove entry\n +S for search\nQ to quit\n"; chomp ($option = <STDIN>); if ($option eq "l") { &entrylist; } elsif ($option eq "a") { &addentry; } elsif ($option eq "r") { &unavailable; #&removeentry; } elsif ($option eq "s") { &unavailable; #&search; } elsif ($option eq "q") { $x = 2; } else { print "Invalid Entry!"; } } sub entrylist { system "cls"; ### Open the filehandle for the address database, sort it. open ADDYBASE, "addybase.adb" or die "\nRequired source database file does not exist!"; foreach (<ADDYBASE>) { $ent++; chomp ($line = $_); @entry = split /:/, $line; # Now that they're separated, store them into individual arrays $t_entry = pop @entry; push (@email, $t_entry); $t_entry = pop @entry; push (@fname, $t_entry); $t_entry = pop @entry; push (@lname, $t_entry); } close ADDYBASE; ### Ensure you start at 0 $ent -=1; for (0..$ent) { print "\n", $lname[$_],", ", $fname[$_], " -- ", $email[$_]; } &orquit; } sub orquit { print "\n\nPress enter to continue, enter Q to quit\n"; chomp ($option = <STDIN>); $x = 2 if ($option eq "q"); } sub unavailable { print "This function is not yet available. Sorry."; &orquit; } sub addentry { open ADDYBASE, ">>addybase.adb"; @addtemp = undef; system "cls"; print "First name?\n"; chomp ($addfname = <STDIN>); push (@addtemp, $addfname); print "\nLast name?\n"; chomp ($addlname = <STDIN>); push (@addtemp, $addlname); print "\nEmail address?\n"; chomp ($addemail = <STDIN>); push (@addtemp, $addemail); $addstring = (join ":", @addtemp); print ADDYBASE ("\n", $addstring); close ADDYBASE; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Learning Exercise
by Abigail-II (Bishop) on Jul 18, 2002 at 13:18 UTC | |
by Foncé (Scribe) on Jul 18, 2002 at 13:32 UTC | |
Re: Learning Exercise
by atcroft (Abbot) on Jul 18, 2002 at 12:52 UTC | |
by Foncé (Scribe) on Jul 18, 2002 at 13:02 UTC | |
by Spudnuts (Pilgrim) on Jul 18, 2002 at 14:42 UTC | |
Re: Learning Exercise
by moxliukas (Curate) on Jul 18, 2002 at 12:46 UTC | |
by Foncé (Scribe) on Jul 18, 2002 at 12:50 UTC | |
Re: Learning Exercise
by Abstraction (Friar) on Jul 18, 2002 at 13:39 UTC | |
by Foncé (Scribe) on Jul 18, 2002 at 13:58 UTC | |
Re: Learning Exercise
by fsn (Friar) on Jul 18, 2002 at 14:59 UTC | |
by Foncé (Scribe) on Jul 18, 2002 at 15:19 UTC | |
Re: Learning Exercise
by PhiRatE (Monk) on Jul 19, 2002 at 00:28 UTC | |
Re: Learning Exercise
by debiandude (Scribe) on Jul 18, 2002 at 18:55 UTC | |
by runrig (Abbot) on Jul 18, 2002 at 20:16 UTC | |
by Foncé (Scribe) on Jul 18, 2002 at 19:45 UTC | |
by jarich (Curate) on Jul 19, 2002 at 01:06 UTC | |
by debiandude (Scribe) on Jul 19, 2002 at 13:16 UTC | |
by Foncé (Scribe) on Jul 19, 2002 at 13:24 UTC | |
Re: Learning Exercise
by John M. Dlugosz (Monsignor) on Jul 18, 2002 at 17:55 UTC |
Back to
Seekers of Perl Wisdom