in reply to GLOB function

The glob function has issues, see To glob or not to glob. But if your pattern is always going to be the fixed string you showed, without variables interpolated into the string, then it's probably ok in this case.

When you say "files with unique names", I'm going to assume you mean basenames (the filename without the path), but I'm also guessing that what you need in @filelist is still the full filename, so that you can properly open the files. Also, by "latest updated/created", I'm guessing you mean the mtime (see also stat(2)).

You could use a hash; here I'm using the basename as the key, and each value is an anonymous array where the first element is the full filename and the second element is the mtime. To see how it works, uncomment the "# Debug" lines.

use warnings; use strict; #use Data::Dump; # Debug use File::Basename qw/fileparse/; use File::stat; my %filelist; for my $file (glob '/user/*/ws/*/BLK_*/*.txt') { my $bn = fileparse($file); my $mtime = stat($file)->mtime; #dd $file, $bn, $mtime; # Debug $filelist{$bn} = [$file,$mtime] unless $filelist{$bn} && $filelist{$bn}[1]>$mtime; } #dd \%filelist; # Debug my @filelist = sort map {$_->[0]} values %filelist; #dd \@filelist; # Debug

Replies are listed 'Best First'.
Re^2: GLOB function
by rahu_6697 (Novice) on Jun 19, 2018 at 05:16 UTC

    Thanks for help, you have understood the problem quite well, I have to sort files with same basename present in different directory and subdirectories according to their mtime such that only latest modified file would come in @filelist. Later I have to work on these files separately to fetch some particular data which is working fine for me.I have done changes as per your code but on terminal these errors are coming. Please resolve this issue as I am working on a shared workspace so I can't install any package in linux.

    Use of uninitialized value $arg in stat at /user/s5e7420/local/lib/per +l5/5.18.0/File/stat.pm line 206. Use of uninitialized value $name in index at /user/s5e7420/local/lib/p +erl5/5.18.0/Symbol.pm line 118. Use of uninitialized value $name in pattern match (m//) at /user/s5e74 +20/local/lib/perl5/5.18.0/Symbol.pm line 121. Use of uninitialized value $name in hash element at /user/s5e7420/loca +l/lib/perl5/5.18.0/Symbol.pm line 121. Use of uninitialized value $name in concatenation (.) or string at /us +er/s5e7420/local/lib/perl5/5.18.0/Symbol.pm line 129. Can't call method "mtime" on an undefined value at script.pl line 13.

      Hello again rahu_6697,

      Fellow Monk haukex have provided a full answer to your question, but I just wanted to add something here. You wrote: Please resolve this issue as I am working on a shared workspace so I can't install any package in linux.

      Programming and troubleshooting is part of the learning curve. Even if the fellow Monk haukex will solve the problem that you have today for you what you have learned? He also gave you a hint in the sample of code Data::Dumper module is for debugging purposes. Did you tried to debug your code? It is part of fun/hell debugging and troubleshooting.

      Regarding the part that you are not able to install modules, Data::Dumper, File::stat and File::Find are core modules.

      #!/usr/bin/env perl use strict; use warnings; use Module::CoreList; my @moduleList = ("Data::Dumper", "File::stat", "File::Find"); foreach my $module (@moduleList) { if (Module::CoreList::is_core($module)) { print "".$module." is a core module and it was released:"; print Module::CoreList->first_release($module) . "\n"; } else { print "".$module." it is not core module!\n"; } } __END__ $ perl test.pl Data::Dumper is a core module and it was released:5.005 File::stat is a core module and it was released:5.004 File::Find is a core module and it was released:5

      Hope this helps, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
      A reply falls below the community's threshold of quality. You may see it by logging in.
        > but I'm not aware of a situation where glob might return undef

        $ perl -wE 'say scalar glob "NONEXISTENT*"' Use of uninitialized value in say at -e line 1.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        I am attaching you the full code that I am executing. Here for every input files I am parsing lines starting with / RULECHECK/ .In each line there are 8 set of entries separated by spaces but i have to insert only 2 and 8th entry in output.txt

        #!/usr/local/bin/perl -w open (OUT, ">output.txt"); use warnings; use strict; #use Data::Dump; # Debug use File::Basename qw/fileparse/; use File::stat; my %filelist; for my $file (glob '/user/*/ws/*/BLK_*/*.txt') { my $bn = fileparse($file); my $mtime = stat($file)->mtime; #dd $file, $bn, $mtime; # Debug $filelist{$bn} = [$file,$mtime] unless $filelist{$bn} && $filelist{$bn}[1]>$mtime; } #dd \%filelist; # Debug my @filelist = sort map {$_->[0]} values %filelist; #dd \@filelist; # Debug for my $file (@filelist) { my $fname = substr($file, rindex($file,"/")+1, length($file)-ri +ndex($file,"/")-1); #for printing file name in my output printf OUT $fname; open (IN, "<",$file); while ($line = <IN>){ chomp($line); if ($line =~/ RULECHECK/){ @temp = split (/\s+/,$line); $start = $temp[2]; $count = $temp[8]; if($count > 1000){ printf OUT "%-60s %-10s\n",$start,$count; } } } } system ("gedit output.txt");