http://qs321.pair.com?node_id=763286


in reply to Pattern Matching in Arrays

One way, which would be slow for directories with lots of files would be something like this. Sort the array of filenames, then use the pars before the _ as hash keys, the elements just overwrite the previous values.
#!/usr/bin/perl use strict; use warnings; my @data = qw/12345_-v1.pdf 12345_Av1.pdf 123456_-v1.pdf 123456_Av1.pd +f 123456_Bv1.pdf/; my %hash; for my $f (sort @data) { my ($key,$version) = split /_/,$f; $hash{$key} = $version; } for my $key (keys %hash) { print $key . '_' . $hash{$key} . "\n"; }
--
Linux, perl, punk rock, cider: charlieharvey.org.uk.