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:

#!/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; }
Thanks in advance!

In reply to Learning Exercise by Foncé

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.