Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How to process several files with different line numbers

by Athanasius (Archbishop)
on Jul 05, 2014 at 14:56 UTC ( [id://1092370]=note: print w/replies, xml ) Need Help??


in reply to How to process several files with different line numbers

Hello thanos1983,

I’m glad to see from your update that the script is now working as you want. However, I still have a couple of observations:

  1. You open the filehandle READ for reading ("<"), but then try to write to it:

    if (-z "".$arg."") { print READ "is empty!\n";

    If the test ever succeeds (because the file is empty), the code will break.

  2. while ( my @doc_read = <READ> ) {

    Assigning the output of the diamond operator to an array puts the operation into list context, which means the entire file is read on the first iteration of the loop. So there is no point in having a while loop here.

  3. Calling a subroutine by prefixing an ampersand ( &second(@ARGV); ) is usually a bad idea, unless you really need to disable prototyping, in which case you have to omit the trailing parentheses (see What's the difference between calling a function as &foo and foo()?). Prefer an ordinary subroutine call: second(@ARGV);

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: How to process several files with different line numbers
by thanos1983 (Parson) on Jul 05, 2014 at 15:43 UTC

    Hello Athanasius

    It seems that we have the same name.

    1. I am applying this condition to check if the file is empty otherwise to break. I am not trying to the file after, unless I do not understand what you mean with write.

    2. You are absolutely right about that I did not even think about it since I am putting the whole file into an array what is the point of the While () condition. Thanks

    3. I was not aware for that thanks for telling me. I am still on the learning curve. :D

    Seeking for Perl wisdom...on the process...not there...yet!
      ...unless I do not understand what you mean with write.

      The line in question is:

      print READ "is empty!\n";

      This syntax has the form print FILEHANDLE LIST, as documented in print, and it says: print the string “is empty!”, followed by a newline, to the filehandle READ. But, as I pointed out, the READ filehandle has been opened for reading (input), not writing (output). The only reason you are not seeing this die is that the files you are testing aren’t empty, so the condition never evaluates to true and the print statement is never actually called.

      You probably meant to write just print "is empty!\n"; which is equivalent to print STDOUT "is empty!\n";, but you should also consider using warn "is empty!"; which is equivalent to print STDERR "is empty!\n"; (see warn).

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Hello Athanasius,

        Oh my mistake you are absolutely right, I do not notice that believe it or not. I was thinking something completely different, because I copied this part of another script that I used and I had in my mind by READ the name of the file.

        Thanks for pointing out, I should modify it now. :D

        Seeking for Perl wisdom...on the process...not there...yet!
      print READ "is empty!\n";

      as Athanasius points out: this tries to write "is empty!" to the file READ.

      Probably what you intended was to print the file name with that message instead of (mis)using the file handle - should look like

      print "$arg is empty!\n";

        Hello soonix,

        Apologies, my brain was not working at the moment. I was really thinking how to solve a problem that I have with arrays of arrays and I completely forgot the meaning of the print READ. I had in my mind the name of the file as you correctly pointed out.

        Thanks for your time and effort to clear it out though.

        Seeking for Perl wisdom...on the process...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1092370]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found