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

I have a code that gets a parameter from a form.
And it should look in the text file if there is any line match at the begging of the line with the param passed from the form, if it does, don't save to the file it should go to
another part of the program. If it does not match write new entry to the end of the file with
new name at the beginning of the file and
the rest of the line with values like
thenewvaluehere*1*1*1*1*1*1*1*1*1*1
Here is the code but it's not working, I need help!!!!
First hte sample test file:
TTMIS*1*1*sandbox*1*1*1*1*1*1*1 Joe Smith*1*1*sandbox*1*1*1*1*1*1*1 Company*1*1*sandbox*1*1*1*1*1*1*1 Travel*1*1*sandbox*1*1*1*1*1*1*1 Comerce*1*1*sandbox*1*1*1*1*1*1*1

Now the code:
my $ag = param("name");#a name comming from the form my $filename="template_data_test.txt"; my $template_data = "/cgi-bin\\content\\unsecure\\".$filename; open(DATA_IN, "$template_data") || print "Can't open output file1: $te +mplate_data\n"; while (<DATA_IN>) { $_ =~/^(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\ +*(.*?)\*(.*?)\*(.*?)$/; my $value1=$1; unless($value1 ne $ag) { s/$value1/$ag/g; # I might have a problem here cause it ch +anges every value of $value1 $save_list=$save_list.$_;}else{$save_list=$save_list.$ +_;} } open( DATA_OUT, ">>test2.txt" ) or die "$!\n"; print DATA_OUT "$save_list"; close DATA_IN; close DATA_OUT;

Replies are listed 'Best First'.
Re: Text File Problem
by tachyon (Chancellor) on Nov 06, 2002 at 21:43 UTC

    The first obvious problem you have is that this...

    my $template_data = "/cgi-bin\\content\\unsecure\\".$filename;

    has absolutely no chance of being a valid path on any system I know of. As a result your open will fail and as you appear not to have printed a header? you are probably getting a 500 internal server error. "Does not work" is self evident....I suggest you read CGI Help Guide and add in some debugging code and report the error message if you want some more useful help.

    Something like this might be a good starting point:

    #!/usr/bin/perl -w # ensure all fatals go to browser during debugging and set-up # comment this BEGIN block out on production code for security BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); } use CGI ':standard'; my $find = param("name"); my $filename = "template_data_test.txt"; my $fullpath = "/full/path/to/cgi-bin/content/unsecure/" . $filename +; open IN, $fullpath or die_nice("Can't open $fullpath, perl says $!\n") +; my $found_it = 0; while (<IN>) { /^([^*])\*/; if ( $find eq $1 ) { $found_it = 1; last; } } close IN; if ($found_it) { # do found it stuff print "Found it!\n"; } else { # do didn't find it stuff print "Did not find it!\n"; } sub die_nice { print "Content-type: text/html\n\n"; print "Error " . shift; require Cwd; print "By the way the current working directory is " . Cwd::getcwd +(); print "\nYou can use this to get your path right!"; exit; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Text File Problem
by buckaduck (Chaplain) on Nov 06, 2002 at 22:10 UTC
    # Your code $_ =~ /^(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?) +\*(.*?)\*(.*?)$/; my $value1=$1;

    That is a very complicated way of doing what you really want, which is probably this:

    # My code my ($value1) = split /\*/;

    Next problem:

    # Your code unless($value1 ne $ag) { s/$value1/$ag/g; $save_list=$save_list.$_; }
    The s/// substitution is useless. This block of code only executes when $value1 equals $ag. So you're replacing the value with itself! I think you want if() rather than unless(). And you're probably right about removing the /g flag.

    While we're at it, your $value1 could have special regex characters, so you should protect them with \Q..\E syntax:

    # My code if ($value1 ne $ag) { s/\Q$value1\E/$ag/; $save_list=$save_list.$_; }

    Update: Added \Q..\E recommendation.

    buckaduck

Re: Text File Problem
by sch (Pilgrim) on Nov 06, 2002 at 21:13 UTC

    OK, so what happens if you try and run the code - do you get errors or does it just behave in an unexpected way - if so can you give us an example of how it fails.

    From looking at the comment in the code, it seems that there's a problem in the substitution - if so what happens if you leave off the /g at the end?

    But today you took me walking, Through a land that we have lost,
    While our children sit at websites, With no access to the cost