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

Hi - Having learned so much from just a few pointers from a few kind Monks, I once again turn to the wealth of expertise in cyberspace!

My situation: A file gets saved by the client in the following format:

[username][language code].[what](optional [pub])
fred2.description            (not public)
fred2.description.published  (public)

I've successfully managed to isolate the language code (2 in this case) without problem. However, what I'd like to find out is, if it is possible to push into an array the language code and another $variable which points to the flag of that particular language, and if so, how.

I've got something like this in mind;

@array = 2 $flag_eng 3 $flag_fre etc...

The intention is to use the array at a later stage in the script to display what languages the page is available in and obviously display the flag as the link.

Any advice/assistance will always be greatly appreciated.

Richard.

Replies are listed 'Best First'.
Re: Pushing Filename parts into an array
by the pusher robot (Monk) on Oct 31, 2002 at 00:12 UTC
    You should probably have a hash with the language codes as keys and the flag for each language as the value. Your array would then just contain the codes. When you wanted to display what languages were available, you would loop through the array and use the hash to find the flag for each language.
Re: Pushing Filename parts into an array
by ehdonhon (Curate) on Oct 31, 2002 at 00:13 UTC

    Do you know that your language code is always numeric and the user names never contain numerals? If not, you will run into problems. The other solutions would be to specify user names and language codes with delimeters or in fixed width format

    Here's one possible solution:

    my ($user, $lang, $desc, $published, $public ) = $line =~/([^\d]+)(\d+)\.([^\.]+)\.([^\.])\s+\(([^\)])\)/;
Re: Pushing Filename parts into an array
by roik (Scribe) on Oct 31, 2002 at 08:51 UTC
    Try using a hash:
    use strict; my %flags; $flags{"1"} = "french"; $flags{"2"} = "german"; # The hash index can be any string, not just numeric $flags{"dutch"} = "Dutch"; # You can print a hash element by accessing it directly print "Single flag: " , $flags{2} , "\n"; # Or you can loop through the hash. Note that the order # the keys are listed in the has is not specified foreach my $key (keys %flags) { print $flags{$key} . "\n"; }