2

I am looking for a more elegant way to create a hash that contains the list I have read in from my configuration file. Here is my code:

read_config($config_file => my %config);

my $extension_list_reference = $config{extensions}{ext};

my @ext;

# Store each item of the list into an array

for my $i ( 0 .. (@$extension_list_reference - 1) ) {
    $ext[$i] = $extension_list_reference->[$i];
}

# Create hash with the array elements as the keys

foreach my $entry (@ext) {
    $extensions{$entry} = "include";
 }   

Thanks.

4
  • I'm going to give you the benefit of the doubt and assume you just forgot to include the "use strict;" and "use warnings;" that are surely at the top of your code. Commented May 27, 2009 at 11:59
  • One of the most frustrating things I found about Perl when I first learned it is that strict wasn't on by default. I'll throw them in for completeness. Commented May 27, 2009 at 14:39
  • Oh wait, this isn't my question. It's some other Neil's question. oops! Commented May 27, 2009 at 14:40
  • We should do something so all us "Neil"s don't get confused! Can be update our names? May I suggest you become "Brooklyn Neil" and I'll become "D.C. Neil". Commented May 27, 2009 at 18:37

4 Answers 4

13

my %hash = map { $_ => 'include' } @list;

Sign up to request clarification or add additional context in comments.

1 Comment

The following worked: %extensions = map {$_ => "include" } @$extension_list_reference;
6

Try using map: http://perldoc.perl.org/functions/map.html

Here's what your new code should look like:

my %extensions = map { $_ => "include" } @{ $config{extensions}{ext} };

3 Comments

Thanks. How's Brooklyn? My birthplace.
It's nice, just moved here 4 months ago.
my %extensions = map { $_ => "include" } @{ $config{extensions}{ext} }
2

If I understand your problem, this is how you do it in one line:

@extensions{@$extension_list_reference} = ();

Note: each value of the hash is empty, but you still can check whether the key exists in the hash using function exists, like this:

if(exists $extensions{$some_key}) {...

P.S. If by some reason you really need those strings 'include' as values, you can have them, too:

@extensions{@$extension_list_reference} = ('include') x @$extension_list_reference;

12 Comments

Why does this work instead of : %extensions{@$extension_list_reference} = (); ?
Because you are operating on a 'hash slice': perldoc.perl.org/perldata.html#Slices
This works fine when the list has more than one value. It's possible that someone could have only one value in their configuration file. How could I handle one value versus many values?
I'm sorry I forgot to include the error message I get if the list contains only one item: Can't use string (".frames") as an ARRAY ref while "strict refs" in use at create_video_frames.pl line 209 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref. Uncaught exception from user code: Can't use string (".frames") as an ARRAY ref while "strict refs" in use at create_video_frames.pl line 209. at create_video_frames.pl line 209
You can check what $extension_list_reference points by using the ref function. So $extension_list_reference = ref $extension_list_reference ? $extension_list_reference : [ $extension_list_reference ]; ought to do it.
|
1

This way:

read_config($config_file => my %config);
%extensions = map +($_ => "include"), @{$config{extensions}{ext}};

or this way:

read_config($config_file => my %config);
@extensions{@{$config{extensions}{ext}}} = ("include") x @{$config{extensions}{ext}};

1 Comment

I like this approach - Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.