in reply to replace a value in an array

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.

Replies are listed 'Best First'.
Re^2: replace a value in an array
by AnomalousMonk (Archbishop) on Apr 02, 2014 at 21:58 UTC
    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.)