You seem to be suggesting that different entries may have different numbers of fields to be entered. How is the true field count determined for a given entry, and where to the field labels come from? (In your example, there are just two fields per entry, with the labels "id" and "comments".)

You also seem to want the user to be able to write incomplete entries, escaping out of the field-entry loop with only one or more of the initial fields filled in, and one or more of the final fields in the sequence left undefined (null), and these get written to the database file along with complete entries.

I think what you might want here is a subroutine that takes two array refs, one containing the field labels and the other containing prompt strings, with both arrays ordered according to the desired sequence for user input; the sub would return a hash with field labels as keys and user input as values. Whatever is returned by the sub can then be written to the database. Something like this (not tested):

sub entry_prompter { my ( $labels, $prompts ) = @_; my %entry; for my $i ( 0 .. $#$labels ) { my $user_input = prompt $$prompts[$i], -escape; last if ( $user_input eq "\e" ); $entry{$$labels[$i]} = $user_input; } return %entry; }
WIth that, it's up to the caller to set up the appropriate arrays, handle the iteration that prompts for "Add another entry? y/n: ", and read/write the database file.

If I were you, I'd add another array that provides conditions for sanity checks on the fields, so that the "entry_prompter()" sub can test $user_input -- e.g. should be numeric, or "y/n", or not longer than 10 characters, or whatever.

Then again, if I were you, I'd use a real database (not a flat file), and I'd use Tk; to provide a nice GUI with appropriate user input widgets to fully populate a record as efficiently and reliably as possible, along with a "Submit" button.

(updated to simplify some of the wording in the text)


In reply to Re: Custom interrupt? by graff
in thread Custom interrupt? by azredwing

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.