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

Hi all,

I wrote this code in which I want to read some files in a folder. (Files are named in format YYYY_MM_DD,eg. 2010_01_24,). There are around "30 files" with naming from 2009_01_25.dat to 2010_01_24.dat. Now I wish to apply a filter on the date and read only those files which matches the range of dates. The code is given below.

use strict; use warnings; my $csvprint; my $csv; opendir DIR, "./dates/"; my @inputfiles = grep { $_ ne '.' && $_ ne '..' } readdir (DIR); my @sorting = sort {$a cmp $b} @inputfiles; #print @sorting,"\n\n\n"; foreach(@sorting) { my $start = "2009_12_30"; my $end = "2010_01_05"; if ($_=~ /^$start/../^$end/) { open FILE, "< ./dates/$_"; while (<FILE>) { chomp; my @content = split (/\,/,$_); my @datesplit = split(/\_/,$content[0]); $csvprint = "$datesplit[0],$datesplit[1],$content[1]\n"; if ($csvprint ne 0) { $csv .= $csvprint; } else { print "blank\n"; } } } else { print "nothing printed. \n"; #print $_,"\n"; } #print $csvprint,"\n\n"; } #print $csv; open CSV,"> 1.csv"; print CSV $csvprint;

My input files are of the format

File Name : 2009_12_24.dat

Content:
2009-12-24_Image,807
2009-12-24_image,119895419
2009-12-24_x-epoc,1138251
2009-12-24_multipart,185849
2009-12-24_application,45164332
2009-12-24_,5587
2009-12-24_audio,11638246
2009-12-24_video,97898353
2009-12-24_text,65788112


Now when I run this script I am getting this warning everytime.

Use of uninitialized value in concatenation (.) or string at files.pl +line 26, < FILE> line 6. Use of uninitialized value in concatenation (.) or string at files.pl +line 26, < FILE> line 15. Use of uninitialized value in concatenation (.) or string at files.pl +line 26, < FILE> line 24. Use of uninitialized value in concatenation (.) or string at files.pl +line 26, < FILE> line 33. Use of uninitialized value in concatenation (.) or string at files.pl +line 26, < FILE> line 42. Use of uninitialized value in concatenation (.) or string at files.pl +line 26, < FILE> line 51. Use of uninitialized value in concatenation (.) or string at files.pl +line 26, < FILE> line 60.
and also I am able to get the results. I am not able to understand why this warning is being generated. Please help.

Replies are listed 'Best First'.
Re: reading files in a directory with range operator.
by toolic (Bishop) on Jan 24, 2010 at 18:39 UTC
    This is your line 6:
    2009-12-24_,5587
    After your 1st split, $content[0] contains the string 2009-12-24_, as desired. Then you split that string on the underscore character. The resulting array, @datesplit, now has only one element. The problem arises because you expected that array to contain (at least) two elements in your next line:
    $csvprint = "$datesplit[0],$datesplit[1],$content[1]\n";
    If this line represents valid input data, then you must check how many elements are in your @datesplit array if you want to avoid this warning. Perhaps you want to do something like this:
    if (@datesplit > 1) { $csvprint = "$datesplit[0],$datesplit[1],$content[1]\n"; } else { $csvprint = "$datesplit[0],$content[1]\n"; }
Re: reading files in a directory with range operator.
by Anonymous Monk on Jan 24, 2010 at 14:13 UTC
    Simple, line 26 in your program
    my @content = split (/\,/,$_); my @datesplit = split(/\_/,$content[0]); $csvprint = "$datesplit[0],$datesplit[1],$content[1]\n";
    fails for line 6 of FILE, which is
    2009-12-24_,5587