EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

hello Monks again, I have the following code :
@CATEGORY = ("USA", "CANADA" , "EUROPE" , "TAIWAN"); @FOOD = ("PIZZA" , "MAPLE" , "EURO" , "INDEPENDENCE");
note that USA correspond to PIZZA and CANADA correspond to MAPLE and etc ... now i sorted the array by highest length to lowest length
@FOOD = ("INDEPENDENCE", "MAPLE", "PIZZA" , "EURO");
I have a lot of file names sorted in @FILENAMES. I want to sort this list based on longest names to shortest (@FOOD) since it's possible to subsets of search names and put them into the individual countries . Example
@FILENAMES = ("MAPLESTEA.txt" , "HOTPIZZA.txt" , "EURODOLLAR.txt"); end result i want to be is $HASH{"CANADA"}{"MAPLESTEA.txt"} = 1; $HASH{"USA"}{"HOTPIZZA.txt"} = 1; $HASH{"EUROPE"}{"EURODOLLAR.txt"} = 1;

Replies are listed 'Best First'.
Re: Organizing Question
by ccn (Vicar) on Sep 03, 2004 at 22:48 UTC

    use strict; my @CATEGORY = ("USA", "CANADA" , "EUROPE" , "TAIWAN"); my @FOOD = ("PIZZA" , "MAPLE" , "EURO" , "INDEPENDENCE"); my @FILENAMES = ("MAPLESTEA.txt" , "HOTPIZZA.txt" , "EURODOLLAR.txt"); my %HASH; my %aux; @aux{@FOOD} = @CATEGORY; LINE: foreach my $file (@FILENAMES) { foreach (sort {length($b) <=> length($a)} keys %aux) { if ($file =~ /\Q$_\E/) { $HASH{$aux{$_}}{$file} = 1; next LINE; } } } use Data::Dumper; print Dumper(\%HASH);
Re: Organizing Question
by sintadil (Pilgrim) on Sep 03, 2004 at 22:48 UTC

    EchoAngel, perhaps you should have a look at mapcar (for mapping the array elements as you want) and the recently-answered array sorting by length question.

    UPDATE: Whoops, sintadil should double check his URLs before posting.

Please pick a more descriptive title
by FoxtrotUniform (Prior) on Sep 03, 2004 at 22:36 UTC

    EchoAngel:

    Please try to choose better titles for your nodes. More descriptive node titles are more likely to attract people who both want to and can answer your questions. They also make it more likely that other people with similar questions will be able to find answers by searching through old threads. Have a look at How do I compose an effective node title?.

    --
    F o x t r o t U n i f o r m
    Found a typo in this node? /msg me
    % man 3 strfry