Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: sorting a filelist array by filename

by tweetiepooh (Hermit)
on May 19, 2009 at 16:01 UTC ( [id://764962]=note: print w/replies, xml ) Need Help??


in reply to sorting a filelist array by filename

Hashes are a good way to handle uniqueness. Look up "hash of arrays".

Here is some code that illustrates some of what you want.

#!/usr/local/bin/perl -w use 5.010; use strict; ### code from here ### my @classedSources = ( "hitdb/CustomerPartner/Opten/Scripts/Daily/egy_batch.sql", "hitdb/CustomerPartner/Opten/Scripts/Daily/megegy_batch.sql", "hitdb/CustomerPartner/Opten/Scripts/Other/cp_OptenTruncTables.sql", "hitdb/CustomerPartner/Opten/Scripts/Other/htcp_firm_loadAll.sql", "hitdb/CustomerPartner/Opten/Scripts/Other/runHT_CP_EKN.sql", "hitdb/CustomerPartner/Opten/Scripts/Other/runHT_CP_FIRM.sql", "hitdb/Policy/Views/Types/HtTypoWsAlaTarifFE1.sql", "hitdb/Policy/Types/HtTypoWsHobMfactorFE1.sql", "hitdb/Policy/Types/HtTypoWsCoverAlaFE1.sql", "hitdb/Policy/Views/Types/HtTypoWsCoverAlaFE1.sql", "hitdb/Policy/Types/HtTypoWsCoverAlaListFE1.sql", "hitdb/Policy/Views/Types/HtTypoWsCoverAlaListFE1.sql", "hitdb/Security/Scripts/ht_tut_mail_address_ws.sql", "hitdb/Policy/Views/Types/HtTypoWsHobMfactorFE1.sql", "hitdb/Accounting/Scripts/ht_tut_mail_address_ws.sql", "hitdb/Policy/Scripts/ht_tut_mail_address_ws.sql", "hitdb/Policy/Types/HtTypoWsAlaTarifFE1.sql", "hitdb/astools/as#arch/Setup/Scripts/ht_tut_mail_address_ws.sql" ); my %hash; # this loop parses the array, splitting entries into path and filename # then populating a hash of arrays keyed on filename foreach (@classedSources) { my ($path,$file) = $_ =~ /(.*\/)(.*)/; push @{ $hash{$file} }, $path; } # parse the hash of arrays listing out the paths for each filename foreach my $file (sort keys %hash) { my @array = @{$hash{$file}}; say $file; foreach my $path (@array) { say "\t$path" } }

Replies are listed 'Best First'.
Re^2: sorting a filelist array by filename
by Vasek (Acolyte) on May 19, 2009 at 23:19 UTC
    Dear tweetiepooh, thanks a lot for your clear and useful code. Exactly that was what I needed! Anyway util's code is very similar, but roubi uses the File::Basename modul which I should avoid in this case, cause I must make a very pure and native perl code, without using any modul.

    Thx for all, again!
      the File::Basename modul which I should avoid in this case, cause I must make a very pure and native perl code, without using any modul
      File::Basename is a Core module which is included with every installation of Perl. There is no need to download and install it from CPAN, if that's what you're worried about.
        Hi toolic,

        thanks for your reflection on using the File::Basename. You're right, and I use it quite frequently, anyway, but in this case I'll have to compile my script to binary (os: win) and as I use tinyperl (free) for compiling, which put all necessary items into a LibZip. I can awoid using LibZip if I omit using any modul. That is the real reason of my fighting against the excellent File::Basename modul.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://764962]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found