in reply to count multiple variables in a single array.
I would not as a first thought change into the directory (chdir). Instead, I would run glob on the target directory from the current program directory. You have the full path information to do that. The problem with chdir is that you have to keep track of "where you are". In a longer program this can become problematic, especially in the case of some path error.
You seem to have a file naming convention for files in this error directory with "userName-fileName.extn". I would not overly specify the .extns that could be in this directory. I allow any extension below (tiff, psd, whatever).
I would of course not "hard code" the user names, which I think is part of your question. I use a HoA (Hash of Array) below. I made a few files underneath a test directory on my Windows machine, keyed to each discovered user name.
A long time ago, I would have recommended against using glob() because there were 3 distinct versions and it wouldn't be clear which version you have. Now things have become more standardized and I think glob() is fine.
use strict; use warnings; use Data::Dumper; my $path ='c:/test'; my %files_per_user; #HOA foreach my $file (glob("$path/*")) { next unless -f $file; # skip directories # if any exist my ($user,$detail_name) = split ('-',$file,2); push @{$files_per_user{$user}},$detail_name; } print Dumper \%files_per_user; foreach my $user (sort keys %files_per_user) { print "",(split("/",$user))[-1],"\n"; print " $_\n" for @{$files_per_user{$user}}; } __END__ What the TEST Directory looks like: Directory of C:\test 02/26/2020 09:52 PM <DIR> . 02/26/2020 09:52 PM <DIR> .. 02/26/2020 09:22 PM 0 maur-1110.tiff 02/26/2020 09:21 PM 0 maur-1111.psd 08/05/2019 01:14 AM <DIR> subdirtest 02/26/2020 09:20 PM 0 TUMI-1354839054_alt1_.psd 3 File(s) 0 bytes $VAR1 = { 'c:/test/TUMI' => [ '1354839054_alt1_.psd' ], 'c:/test/maur' => [ '1110.tiff', '1111.psd' ] }; TUMI 1354839054_alt1_.psd maur 1110.tiff 1111.psd
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: count multiple variables in a single array.
by flieckster (Scribe) on Feb 28, 2020 at 18:41 UTC | |
by holli (Abbot) on Feb 29, 2020 at 14:13 UTC | |
by Your Mother (Archbishop) on Feb 28, 2020 at 19:43 UTC | |
by Marshall (Canon) on Feb 28, 2020 at 23:11 UTC | |
by Marshall (Canon) on Feb 28, 2020 at 19:37 UTC |