It is a bit hard for me to understand your complete specification. However, here is an attempt and some advice.

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

In reply to Re: count multiple variables in a single array. by Marshall
in thread count multiple variables in a single array. by flieckster

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.