in reply to Re^2: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE
in thread Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE

Isn't while (@files) a valid statement meaning iterate on every item of array @files?

It is a valid statement, but it does not mean what you think it means. It iterates as long as @files is true, i.e., forever (since you do not modify @files). Again, I implore you to go read perlsyn (and/or a good perl book) and learn something.

Replies are listed 'Best First'.
Re^4: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE
by Nik (Initiate) on Dec 31, 2007 at 21:25 UTC
    I was under the impression that 'while(@files)' iterates over each item of the array as long as @files is true, which means as long as it reaches up to the last item of tha array @files. ps. I have a book and read a few things last year but i guess that i need to read more...
      Yes you should probably read more of that book :-)

      Anway: an array only becomes false when it's empty. while (@array) will either not loop at all if the array is empty or loop forever until the array is empty.

      while (<filehandle>) is a special case. all other construct with while(something) just test if something is true.

        Well, it's only special in that it's silently twiddled into while( defined( $_ = <filehandle> ) ) { ... }; it otherwise is still looping until the value of the expression is false (the twiddling being done so that lines that would otherwise be interpreted a boolean false (e.g. 0\n) are differentiated from the diamond-operator-returns-undef-on-eof false value).</pedant>

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        Yeah that why i used it in the 1st place while (<filehandle>) read a file line be line and only went false if there were no ther lines left so it escaped the llop

        while (@files) i believed would do the same thing as while (<filehandle>) did, except from insteading of reading a file line-by-line up ot the end i thought it would read an item-by-item over @array until the end of items and then go false.

      Compare the results of running
      perl -e 'print "$_\n" for (1,2,3,4)'
      to
      perl -e 'print "$_\n" while (1,2,3,4)'
      See what kind of results you get... The truth is that, no, 'while' does not iterate over the list/array, it loops while it is true (evaluated as a boolean). What you're looking for is the 'for' statement.

      -Paul

        I got this error myabe because iam on windows
        C:\Documents and Settings\nik>perl -e 'print "$_\n" for (1,2,3,4)' Can't find string terminator "'" anywhere before EOF at -e line 1.
        even if i switch single quotes with double quotes iam still getting erros.

        I used the foreach statement finally.

        You say while loops over na @array while it is true.(evaluated as a boolean)? what do you mean?

        iterate!= loop ? I was picturing the statement while(@files) as loop over the array as long as it has items in it, and i though it was escaping after the last one.