Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've been playing with the following code posted by turnstep:
Here's a way to do it if your flatfile consists of one record per line +, with whitespace before the "record number" at the end: #!/usr/bin/perl -- -*-fundamental-*- use strict; use Fcntl qw(:flock); ## Set these as needed... my $flatfile = "flat.txt"; my $num2delete = "12"; my $result = &DeleteRecord($flatfile, $num2delete); if ($result) { print "No records were deleted.\n"; } else { print "Deleted record number $num2delete.\n"; } exit; sub DeleteRecord($$) { my $flatfile = shift; my $num2delete = shift; ## Open the flatfile in read/write mode open(FLATFILE, "+< $flatfile") or die "Could not open $flatfile: $!\ +n"; ## Lock it exclusively flock(FLATFILE, LOCK_EX) or die "Could not lock $flatfile: $!\n"; ## Try and find a match my $found=0; while(<FLATFILE>) { last if /\s+$num2delete$/ and ++$found; } ## If no match, we clean up and return a 1 unless ($found) { close(FLATFILE); ## Also unlocks for you return 1; ## Have the caller handle this case; } ## Mark the spot where the record to be deleted starts my $here = tell(FLATFILE) - length $_; ## Read in the rest of the file: my @slurp = <FLATFILE>; ## Rewind to the spot we marked earlier seek(FLATFILE,$here,0); ## Stick everything else back in, overwriting the file as we go: print FLATFILE @slurp; ## If we don't truncate, the last record will appear twice (think ab +out it) truncate(FLATFILE, tell); ## Close and unlock, and return success to the caller close(FLATFILE); return 0; }
It works as is, but modified to work in HTML I run into problems. I'm using a value based on a radio button to set the number of the record to be deleted but i cant get it to work. If i use a value I type in a text box, it works, but not with the radio button. Any help?

Replies are listed 'Best First'.
Re: modifying records in a delimited text file
by Anonymous Monk on Oct 18, 2000 at 22:47 UTC
    open the file ... while(<USERFILE>) { my($A,$B,$C,$D,$E,$F,$G,$H,$I,$J,$K) = split(/\|/); print qq(<TR>); print qq(<TD>$A</TD> <TD>$B</TD> <TD>$C</TD> <TD>$D</TD> <TD>$E</TD> <TD>$F</TD> <TD>$G</TD> <TD>$H</TD> <TD>$I</TD> <TD>$J</TD>); print qq(<TD><INPUT TYPE="radio" NAME="edit" VALUE="$K"> </TD></TR> ); }
    In TurnStep's DeleteRecord sub I set $num2delete to the "edit param", which in this case is "edit".
      Yes but what is the script actually printing. The code looks fine, but I'm wondering if $K actually contains a value at this point. I would be interested in seeing the actual HTML generated by this script.
      jmac says The value of $K is indeed set to the value in the record
      OK, given the following apparently proven assumptions:

      1. That the radio button being printed is named "edit" and that its value when selected is indeed, say, 5, and that the radio button is indeed selected, the script fails.

      2. That a textual input field of name "edit", given a value of "5", permits the script to succeed

      This just makes no sense to me. How are you parsing the "edit" form element? Is it empty when your script is called with the radio button method but set to "5" (or whatever) when called with the text field method? Are you using the stock CGI.pm module or are you rolling your own parsing? And, finally, it would help if we could see the HTML as printed out by your script. I keep asking for this but I never see it. Can't you just "View Source" and cut/paste? You can leave out the non-form-related content if it's going to be a bit much.. I just want to see the <form> tags, the <input> tags and any other form tag that might use "edit" as its name. Something is not right. You keep insisting that things are being printed correctly, but this is inconsistent with the results you're getting. We need more information.

Re: modifying records in a delimited text file
by Fastolfe (Vicar) on Oct 18, 2000 at 22:03 UTC
    We're entering the realm of HTML and CGI problems. If it works with a text input field but does not work with a radio button, my first instinct is that you do not have your HTML form built correctly.
    <input type='text' name='number'>
    If that works, your corresponding radio buttons should look something like this:
    <input type='radio' name='number' value='1'> <input type='radio' name='number' value='2'> ...
    Verify that this is correct, and if you're still having problems, we need more information. You posted nothing at all CGI or HTML related, so we have nothing to go on in helping you solve your problem.
Re: modifying records in a delimited text file
by Anonymous Monk on Oct 18, 2000 at 22:25 UTC
    Here's the deal, I have a form that displays a table of the delimited text file values ( I load these from the file, each rec has the format A|B|C|D|E|F|G|H| K where "K" is a unique record num. Next to each record in my table, I have a radio button with a value of the corresponding rec num(which is loaded like
    print qq(<INPUT TYPE="radio" NAME="$name" VALUE="$K">);)
    . Based on what radio button the user checks, I pass that value to $num2delete. No Love. If I hard code a value in the VALUE="" part, it works fine.
      So...

      <INPUT TYPE="radio" NAME="$name" VALUE="$K"> no work <INPUT TYPE="radio" NAME="$name" VALUE="5"> works
      Is this accurate? Perhaps $K is not being generated correctly? Could you perhaps paste some of the actual generated HTML (a few lines would be sufficient)?
Re: modifying records in a delimited text file
by AgentM (Curate) on Oct 19, 2000 at 01:52 UTC
    Viewing previous posts, it makes sense that a debug prior to this code segment may be necessary. It seems rather obvious to me (I may be wrong) that $K may be read incorrectly. Give yourself some debug info on it right after you read it or modify it to make sure it's the value you want.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy is responsible for the comments made by AgentM.