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

Hi Monks, any idea on how I can stop empty files from been pushed into an array. I am reading a file and subsetting different part of the file. Each of the subset is saved as a file and they are pushed into an array. Some of these files are empty and I don't want them in the array as they will give error message when I use the files in the array.
  • Comment on Stopping empty files from been pushed into an array.

Replies are listed 'Best First'.
Re: Stopping empty files from been pushed into an array.
by GotToBTru (Prior) on May 13, 2015 at 19:48 UTC

    Very hard to help when we can't see your code. Be sure to follow these guidelines.

    Without seeing, I can guess at a solution: test the file size before you do the push.

    push @filelist,$filename unless -z $filename;
    Dum Spiro Spero
Re: Stopping empty files from been pushed into an array.
by roboticus (Chancellor) on May 13, 2015 at 19:49 UTC

    ogbalex:

    Sure, just don't push the file onto the array if they're empty:

    push @array, $file_name unless length($file_contents) < 1;

    Update: Corrected len to length. (Too much Python today at work...)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      What is len()? :P

        Why, it is of course:

        sub len{ return do { local(@ARGV, $/) = $_[0]; <> } =~ y===c; }

        :P

        Disclaimer: use -s $filename. This code is ineficient and ment as a timtoady

        Your Mother:

        len() is a function that gives you the length of things....


        ...in python

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.