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

This little code works fine it just keeps generating numbers and writing
them to a data base.
#!/usr/bin/perl print "Content-type: text/html\r\n\r\n"; ### Generate some numbers. $now = time(); $nill = time() + 50; $null = time() + 100; ### Then print to prove that worked print "<br><b>Line one from mixed.data = $now"; print "<br><b>Line two from mixed.data = $nill"; print "<br><b>Line three from mixed.data = $null"; ### Fine -- So now I make an array @scale = ($now,$nill,$null); ### And print to prove that works print "<br><b> Print Array = @scale"; ### Fine ### Then I overwrite mixed.data with $data_path = "data/mixed.data"; open(O, ">$data_path"); print O join("\n",@scale),"\n"; close(O);
Now thats all fine writing to mixed data perfectly. However I have a small circle of code that wont behave itself. the simple database ( mixed.dat )just containes
FAST
FASTER
FASTEST
#!/usr/bin/perl print "Content-type: text/html\r\n\r\n"; ### First I read from database. $data_path = "data/mixed.data"; open(DAT, $data_path) || die("Could not open file!"); @rawdata=<DAT>; close(DAT); $now = $rawdata[0]; $nill = $rawdata[1]; $null = $rawdata[2]; ### Then print to prove that worked print "<br><b>Line one from mixed.data = $now"; print "<br><b>Line two from mixed.data = $nill"; print "<br><b>Line three from mixed.data = $null"; ### Fine so far-- So now I make an array @scale = ($now,$nill,$null); ### And print to prove that works print "<br><b> Print Array = @scale"; ### This results in browser. #----------------------------------- # Line one from mixed.data = FAST # Line two from mixed.data = FASTER # Line three from mixed.data = FASTEST # Printed from Array = FAST FASTER FASTEST #------------------------------------ ### Which is all very fine so far. ### Then I overwrite mixed.data with $data_path = "data/mixed.data"; open(O, ">$data_path"); print O join("\n",@scale),"\n"; close(O);
Which results in mixed.data file being written as

FAST

FASTER

FASTEST
With new line entries
The next time I run the script the data base will be written as
FAST


FASTER

with double line entry and FASTEST has dissapeared
The next time I run it only FAST remains.
Oh-- How confusing!
All I want it to do is just keep repeating the database.
As the first little script dose.
Why you may ask.?
Well if I can get it to repeat the database with the array involved
I can then move some element of the array.
I think it is all to do with \n somwhere.
But I cant find it.
God Bless anyone who can.

jdporter added readmore tags

Replies are listed 'Best First'.
Re: Vicious circle.
by japhy (Canon) on Feb 08, 2006 at 03:47 UTC
    Each line you've read from the file contains a newline at the end of it. Either chomp() the lines, or don't add the newlines!
    chomp(my @lines = <INPUT>); # or print OUTPUT @lines;

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      OK But where in the code would I put such <code. chomp(my @lines = <INPUT>); </code> You are fast.
        You'll have to try a little harder than that. You need to chomp() the lines you read from the file. Figure it out.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Vicious circle.
by graff (Chancellor) on Feb 08, 2006 at 04:46 UTC
    To clarify japhy's instructions, you want to make just one of the following changes -- either this:
    ### First I read from database. $data_path = "data/mixed.data"; open(DAT, $data_path) || die("Could not open file!"); @rawdata=<DAT>; close(DAT); chomp @rawdata; ## <<-- add this line here
    or else this:
    ### Then I overwrite mixed.data with $data_path = "data/mixed.data"; open(O, ">$data_path"); print O @scale; ## <<-- don't use 'join "\n",...' close(O);
    Either one alone will fix the problem. (Don't do both.)

    update: I should point out that the first solution will work equally well with your initial test case (setting $now,$nill,$null to internal time values, instead of reading them from a file) -- chomp only removes the input record separator (value of $/) from the end of whatever string(s) you pass to it; when a string does not end with with that value, chomp does not alter it. Meanwhile, just doing "print O @scale" will do the wrong thing if the array elements don't end with newlines.

      Been fighting with that one for two days now.
      Works like a charm now.
      May God Bless you.
      Nasa.