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

Trying to use Perl for the first time. I have put the code together from tutorials I have found on the Internet.I have a text file that has a entry format of wk,name,team If the person submits a new entry for the same week I want to over write the team value in the file. If it is a new entry the file updates fine but if there is an existing entry, the replace code isn't working for me. I read the small file into an array and then try to change the value if appropriate. I have just posted the snippet of code that isn't working. All the other functions the cgi does with the file are ok. The value of $surv is passed ok into this section but the replacement doesn't happen. I have verified the code is dropping correctly into this section but the record stays unchanged. I was just looking for more experienced Perl users to comment on what I was missing since the replacement doesn't happen. Unfortunately my perl experience is just reading tutorials and trial by error.
@raw_data=<survdat>; foreach $survinn (@raw_data) { chomp ($survinn); ($survwk,$survn,$survtm)=split(/,/,$survinn); if ((uc($survn) eq uc($poolm)) && ($survwk == $weekin)) { s/$survtm/$surv/i; seek(survdat,0,0); print survdat @raw_data; } }

Replies are listed 'Best First'.
Re: replace a value in an array
by davido (Cardinal) on Apr 02, 2014 at 18:11 UTC

    I'd like to help, but that doesn't even compile. Is that the problem you're asking about, or do you have some code that does compile, but that doesn't do what you want it to do?

    From what I can decipher from the code posted, I really think you're working too hard at this. If the file is not "huge", just use Tie::File so you don't have to keep worrying about seeking through the file manually. And if the file is huge, use DBI with DBD:SQLite

    Tie::File is part of the core Perl distribution, so you should have it available, and while its performance isn't excellent, it is well debugged, and saves you from having to implement your own flat-file-database solution.


    Dave

Re: replace a value in an array
by toolic (Bishop) on Apr 02, 2014 at 18:12 UTC
    if ((uc($survn) eq uc($poolm)) && ($survwk = $weekin))
    I think you want a numeric compare (==) , not an assignment (=)
    if ((uc($survn) eq uc($poolm)) && ($survwk == $weekin))
Re: replace a value in an array
by ww (Archbishop) on Apr 02, 2014 at 19:09 UTC
    Concurring with davido's suggestion that we need a clearer problem statement (and probably one that seeks to deal with a single topic at a time), here's an answer to question in your node title:
    C:>perl -E "my @x=qw(3 4 5); say @x; $x[2] = E; say @x;" 345 34E

    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.
Re: replace a value in an array
by marinersk (Priest) on Apr 02, 2014 at 20:50 UTC

    First two things to fix:

    1. Your open statement is broken. Count the quotation marks -- I think you'll see this is not a casual problem. See if you can fix that up a bit.
    2. Your use of flock is, I suspect, incorrect, which is understandable given your newness to the language. I recommend commenting that line out until the basic logic is working. Then move into more advanced features like file locking.

    I've taken the liberty of reformatting your code so it is more readable, and adding the requisite use strict; and use warnings; statements. Please use them. They can be painful at first, but it's a good pain -- they teach you exactly what you are doing wrong as you learn the language. Together they comprise the single best debugging tool Perl has, and all you have to do is not be stubborn and they will reward you in droves.

    #!/usr/bin/perl use strict; use warnings; if ($surv ne "") { $survmatch = "n"; open (survdat, "+< /xxx ||cmn::dienice("$lang::open_file_write"); flock (survdat,2); @raw_data=<survdat>; foreach $survinn (@raw_data) { chomp ($survinn); ($survwk,$survn,$survtm)=split(/,/,$survinn); if ((uc($survn) eq uc($poolm)) && ($survwk = $weekin)) { s/$survtm/$surv/i; $survmatch="y"; seek(survdat,0,0); print survdat @raw_data; } } if ($survmatch eq "n") { print survdat "$weekin,$poolm,$surv\r\n"; } } close survdat; exit; __END__ S:\Steve\Dev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail>update1a.pl Unquoted string "survdat" may clash with future reserved word at S:\St +eve\Dev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl lin +e 9. Scalar found where operator expected at S:\Steve\Dev\PerlMonks\P-2014- +04-02@1439-Array-Update-Fail\update1a.pl line 9, near ""+< /xxx ||cmn +: :dienice("$lang::open_file_write" (Missing operator before $lang::open_file_write?) String found where operator expected at S:\Steve\Dev\PerlMonks\P-2014- +04-02@1439-Array-Update-Fail\update1a.pl line 19, near "$survmatch="" (Might be a runaway multi-line "" string starting on line 9) (Missing semicolon on previous line?) Possible unintended interpolation of @raw_data in string at S:\Steve\D +ev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Possible unintended interpolation of @raw_data in string at S:\Steve\D +ev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Unquoted string "survdat" may clash with future reserved word at S:\St +eve\Dev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl lin +e 26. Global symbol "$surv" requires explicit package name at S:\Steve\Dev\P +erlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 6. Global symbol "$survmatch" requires explicit package name at S:\Steve\ +Dev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 8. syntax error at S:\Steve\Dev\PerlMonks\P-2014-04-02@1439-Array-Update- +Fail\update1a.pl line 9, near ""+< /xxx ||cmn::dienice("$lang::open_f +i le_write" Global symbol "@raw_data" requires explicit package name at S:\Steve\D +ev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survinn" requires explicit package name at S:\Steve\De +v\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "@raw_data" requires explicit package name at S:\Steve\D +ev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survinn" requires explicit package name at S:\Steve\De +v\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survwk" requires explicit package name at S:\Steve\Dev +\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survn" requires explicit package name at S:\Steve\Dev\ +PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survtm" requires explicit package name at S:\Steve\Dev +\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survinn" requires explicit package name at S:\Steve\De +v\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survn" requires explicit package name at S:\Steve\Dev\ +PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$poolm" requires explicit package name at S:\Steve\Dev\ +PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survwk" requires explicit package name at S:\Steve\Dev +\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$weekin" requires explicit package name at S:\Steve\Dev +\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survtm" requires explicit package name at S:\Steve\Dev +\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$surv" requires explicit package name at S:\Steve\Dev\P +erlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$survmatch" requires explicit package name at S:\Steve\ +Dev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 9. Global symbol "$weekin" requires explicit package name at S:\Steve\Dev +\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 26. Global symbol "$poolm" requires explicit package name at S:\Steve\Dev\ +PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 26. Global symbol "$surv" requires explicit package name at S:\Steve\Dev\P +erlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl line 26. syntax error at S:\Steve\Dev\PerlMonks\P-2014-04-02@1439-Array-Update- +Fail\update1a.pl line 28, near "}" S:\Steve\Dev\PerlMonks\P-2014-04-02@1439-Array-Update-Fail\update1a.pl + has too many errors.

      open (survdat, "+< /xxx ||cmn::dienice("$lang::open_file_write");
      ...
      1. Your open statement is broken. Count the quotation marks...

      That's not the only problem: parens are unbalanced, too. Maybe something like
          open (my $survdat, '+<', '/xxx') || cmn::dienice("$lang::open_file_write");
      with  survdat replaced with  $survdat in the code thereafter might be closer to the mark, but hard to tell. (And I'm not sure just "$lang::open_file_write" is supposed to be.)