Match for today's date doesn't match correctly












0















i'm writing a basic Perl script to check if there're files in certain directories their names contain today's date, the script works fine when there are files in the directory and returns TRUE, but doesn't return FALSE when no files withe the given criteria exist



the script is as below:



#!/usr/bin/perl -w
use POSIX qw(strftime);
my $datestring = strftime "%Y%m%d", localtime;
opendir(DIR, 'C:Perl');
@files = grep (/$datestring/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
my $count = () = $file =~ /$datestring/;
if ($count > 0) {
print ("TRUE");
}
else
{
print ("FALSE");
}
}


the files names are like



export_opportunities_20181111-040005_20181124-040010.csv
export_opportunities_20181111-040005_20181122-040010.csv









share|improve this question




















  • 2





    All files meet the criteria because you already filtered them with grep.

    – toolic
    Nov 22 '18 at 16:13


















0















i'm writing a basic Perl script to check if there're files in certain directories their names contain today's date, the script works fine when there are files in the directory and returns TRUE, but doesn't return FALSE when no files withe the given criteria exist



the script is as below:



#!/usr/bin/perl -w
use POSIX qw(strftime);
my $datestring = strftime "%Y%m%d", localtime;
opendir(DIR, 'C:Perl');
@files = grep (/$datestring/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
my $count = () = $file =~ /$datestring/;
if ($count > 0) {
print ("TRUE");
}
else
{
print ("FALSE");
}
}


the files names are like



export_opportunities_20181111-040005_20181124-040010.csv
export_opportunities_20181111-040005_20181122-040010.csv









share|improve this question




















  • 2





    All files meet the criteria because you already filtered them with grep.

    – toolic
    Nov 22 '18 at 16:13
















0












0








0








i'm writing a basic Perl script to check if there're files in certain directories their names contain today's date, the script works fine when there are files in the directory and returns TRUE, but doesn't return FALSE when no files withe the given criteria exist



the script is as below:



#!/usr/bin/perl -w
use POSIX qw(strftime);
my $datestring = strftime "%Y%m%d", localtime;
opendir(DIR, 'C:Perl');
@files = grep (/$datestring/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
my $count = () = $file =~ /$datestring/;
if ($count > 0) {
print ("TRUE");
}
else
{
print ("FALSE");
}
}


the files names are like



export_opportunities_20181111-040005_20181124-040010.csv
export_opportunities_20181111-040005_20181122-040010.csv









share|improve this question
















i'm writing a basic Perl script to check if there're files in certain directories their names contain today's date, the script works fine when there are files in the directory and returns TRUE, but doesn't return FALSE when no files withe the given criteria exist



the script is as below:



#!/usr/bin/perl -w
use POSIX qw(strftime);
my $datestring = strftime "%Y%m%d", localtime;
opendir(DIR, 'C:Perl');
@files = grep (/$datestring/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
my $count = () = $file =~ /$datestring/;
if ($count > 0) {
print ("TRUE");
}
else
{
print ("FALSE");
}
}


the files names are like



export_opportunities_20181111-040005_20181124-040010.csv
export_opportunities_20181111-040005_20181122-040010.csv






perl date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:19









choroba

155k14141204




155k14141204










asked Nov 22 '18 at 16:10









Kareem HamedKareem Hamed

1114




1114








  • 2





    All files meet the criteria because you already filtered them with grep.

    – toolic
    Nov 22 '18 at 16:13
















  • 2





    All files meet the criteria because you already filtered them with grep.

    – toolic
    Nov 22 '18 at 16:13










2




2





All files meet the criteria because you already filtered them with grep.

– toolic
Nov 22 '18 at 16:13







All files meet the criteria because you already filtered them with grep.

– toolic
Nov 22 '18 at 16:13














2 Answers
2






active

oldest

votes


















1














You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



@files = grep (/$datestring/,readdir(DIR));
# this is more commonly written
# @files = grep {/$datestring/} readdir(DIR);

print @files > 0 ? "TRUE" : "FALSE";
# or
print scalar(@files)." files matched $datestring";


See also List::Util.



use v5.10;    
use List::Util qw<all any none>;
###
@files = readdir(DIR);
say 'all match' if all {/$datestring/} @files;
say 'at least one match' if any {/$datestring/} @files;
say 'no matches' if none {/$datestring/} @files;





share|improve this answer































    1














    As commented by toolic, when you are already removing non-matching files here :



    @files = grep (/$datestring/,readdir(DIR));


    Hence your for loop will never see non-matching files.



    Here are a few other comments on your code :




    • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


    • always check the return code of system calls such as opendir


    • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


    • you don't need to use parentheses around argument to the built-in print function



    Here is a cleaner version of your code :



    #!/usr/bin/perl -w
    use strict;
    use warnings;
    use POSIX qw(strftime);

    my $datestring = strftime "%Y%m%d", localtime;
    opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
    my @files = readdir(DIR);
    closedir(DIR);

    foreach my $file (@files) {
    if ($file =~ /$datestring/) {
    print "TRUEn";
    } else {
    print "FALSEn";
    }
    }





    share|improve this answer


























    • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches

      – Kareem Hamed
      Nov 23 '18 at 11:38











    • @Kareem ok I edited the answer to produce the desired output

      – GMB
      Nov 24 '18 at 9:52











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434758%2fmatch-for-todays-date-doesnt-match-correctly%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



    @files = grep (/$datestring/,readdir(DIR));
    # this is more commonly written
    # @files = grep {/$datestring/} readdir(DIR);

    print @files > 0 ? "TRUE" : "FALSE";
    # or
    print scalar(@files)." files matched $datestring";


    See also List::Util.



    use v5.10;    
    use List::Util qw<all any none>;
    ###
    @files = readdir(DIR);
    say 'all match' if all {/$datestring/} @files;
    say 'at least one match' if any {/$datestring/} @files;
    say 'no matches' if none {/$datestring/} @files;





    share|improve this answer




























      1














      You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



      @files = grep (/$datestring/,readdir(DIR));
      # this is more commonly written
      # @files = grep {/$datestring/} readdir(DIR);

      print @files > 0 ? "TRUE" : "FALSE";
      # or
      print scalar(@files)." files matched $datestring";


      See also List::Util.



      use v5.10;    
      use List::Util qw<all any none>;
      ###
      @files = readdir(DIR);
      say 'all match' if all {/$datestring/} @files;
      say 'at least one match' if any {/$datestring/} @files;
      say 'no matches' if none {/$datestring/} @files;





      share|improve this answer


























        1












        1








        1







        You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



        @files = grep (/$datestring/,readdir(DIR));
        # this is more commonly written
        # @files = grep {/$datestring/} readdir(DIR);

        print @files > 0 ? "TRUE" : "FALSE";
        # or
        print scalar(@files)." files matched $datestring";


        See also List::Util.



        use v5.10;    
        use List::Util qw<all any none>;
        ###
        @files = readdir(DIR);
        say 'all match' if all {/$datestring/} @files;
        say 'at least one match' if any {/$datestring/} @files;
        say 'no matches' if none {/$datestring/} @files;





        share|improve this answer













        You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



        @files = grep (/$datestring/,readdir(DIR));
        # this is more commonly written
        # @files = grep {/$datestring/} readdir(DIR);

        print @files > 0 ? "TRUE" : "FALSE";
        # or
        print scalar(@files)." files matched $datestring";


        See also List::Util.



        use v5.10;    
        use List::Util qw<all any none>;
        ###
        @files = readdir(DIR);
        say 'all match' if all {/$datestring/} @files;
        say 'at least one match' if any {/$datestring/} @files;
        say 'no matches' if none {/$datestring/} @files;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 18:02









        beasybeasy

        1,003411




        1,003411

























            1














            As commented by toolic, when you are already removing non-matching files here :



            @files = grep (/$datestring/,readdir(DIR));


            Hence your for loop will never see non-matching files.



            Here are a few other comments on your code :




            • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


            • always check the return code of system calls such as opendir


            • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


            • you don't need to use parentheses around argument to the built-in print function



            Here is a cleaner version of your code :



            #!/usr/bin/perl -w
            use strict;
            use warnings;
            use POSIX qw(strftime);

            my $datestring = strftime "%Y%m%d", localtime;
            opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
            my @files = readdir(DIR);
            closedir(DIR);

            foreach my $file (@files) {
            if ($file =~ /$datestring/) {
            print "TRUEn";
            } else {
            print "FALSEn";
            }
            }





            share|improve this answer


























            • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches

              – Kareem Hamed
              Nov 23 '18 at 11:38











            • @Kareem ok I edited the answer to produce the desired output

              – GMB
              Nov 24 '18 at 9:52
















            1














            As commented by toolic, when you are already removing non-matching files here :



            @files = grep (/$datestring/,readdir(DIR));


            Hence your for loop will never see non-matching files.



            Here are a few other comments on your code :




            • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


            • always check the return code of system calls such as opendir


            • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


            • you don't need to use parentheses around argument to the built-in print function



            Here is a cleaner version of your code :



            #!/usr/bin/perl -w
            use strict;
            use warnings;
            use POSIX qw(strftime);

            my $datestring = strftime "%Y%m%d", localtime;
            opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
            my @files = readdir(DIR);
            closedir(DIR);

            foreach my $file (@files) {
            if ($file =~ /$datestring/) {
            print "TRUEn";
            } else {
            print "FALSEn";
            }
            }





            share|improve this answer


























            • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches

              – Kareem Hamed
              Nov 23 '18 at 11:38











            • @Kareem ok I edited the answer to produce the desired output

              – GMB
              Nov 24 '18 at 9:52














            1












            1








            1







            As commented by toolic, when you are already removing non-matching files here :



            @files = grep (/$datestring/,readdir(DIR));


            Hence your for loop will never see non-matching files.



            Here are a few other comments on your code :




            • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


            • always check the return code of system calls such as opendir


            • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


            • you don't need to use parentheses around argument to the built-in print function



            Here is a cleaner version of your code :



            #!/usr/bin/perl -w
            use strict;
            use warnings;
            use POSIX qw(strftime);

            my $datestring = strftime "%Y%m%d", localtime;
            opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
            my @files = readdir(DIR);
            closedir(DIR);

            foreach my $file (@files) {
            if ($file =~ /$datestring/) {
            print "TRUEn";
            } else {
            print "FALSEn";
            }
            }





            share|improve this answer















            As commented by toolic, when you are already removing non-matching files here :



            @files = grep (/$datestring/,readdir(DIR));


            Hence your for loop will never see non-matching files.



            Here are a few other comments on your code :




            • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


            • always check the return code of system calls such as opendir


            • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


            • you don't need to use parentheses around argument to the built-in print function



            Here is a cleaner version of your code :



            #!/usr/bin/perl -w
            use strict;
            use warnings;
            use POSIX qw(strftime);

            my $datestring = strftime "%Y%m%d", localtime;
            opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
            my @files = readdir(DIR);
            closedir(DIR);

            foreach my $file (@files) {
            if ($file =~ /$datestring/) {
            print "TRUEn";
            } else {
            print "FALSEn";
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 9:51

























            answered Nov 22 '18 at 21:40









            GMBGMB

            9,4922723




            9,4922723













            • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches

              – Kareem Hamed
              Nov 23 '18 at 11:38











            • @Kareem ok I edited the answer to produce the desired output

              – GMB
              Nov 24 '18 at 9:52



















            • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches

              – Kareem Hamed
              Nov 23 '18 at 11:38











            • @Kareem ok I edited the answer to produce the desired output

              – GMB
              Nov 24 '18 at 9:52

















            Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches

            – Kareem Hamed
            Nov 23 '18 at 11:38





            Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches

            – Kareem Hamed
            Nov 23 '18 at 11:38













            @Kareem ok I edited the answer to produce the desired output

            – GMB
            Nov 24 '18 at 9:52





            @Kareem ok I edited the answer to produce the desired output

            – GMB
            Nov 24 '18 at 9:52


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434758%2fmatch-for-todays-date-doesnt-match-correctly%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen