in reply to hash with multiple values per key

It's not entirely clear what format your initial data structure is taking. Are the values a single string? Are an array of values?

Assuming that they are array refs, and that your ranges are ordered from lesser value to greater value, this should work just fine:

my %hash = ( a => ['15-19',30,35,120], b => ['15-17','30-35',40], c => ['15-18',30,35,120], ); for (values %hash) { @$_ = map {/^(\d+)-(\d+)$/ ? ($1..$2) : $_} @$_; } use Data::Dumper; print Dumper(\%hash);
If your ranges could be reversed, then the following adaptation would work:
for (values %hash) { @$_ = map {!/^(\d+)-(\d+)$/ ? $_ : ($1 < $2) ? ($1..$2) : reverse +($2..$1)} @$_; }
- Miller

Replies are listed 'Best First'.
Re^2: hash with multiple values per key
by eric256 (Parson) on Aug 10, 2007 at 23:26 UTC

    This would allow negatives:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $test = { a => '-15-19,30,35,120', b => '15-17,30,35,40', c => '15-18,30,35,120' }; for my $key (keys %$test) { print "Processing '$key'\n"; $test->{$key} = [ map { /(-?\d+)-(-?\d+)/ ? ($1..$2) : 0+$_ } split(',', $test->{$key}) ]; } print Dumper($test);

    And one more with regex:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $test = { a => '-15-19,30,35,120', b => '15-17,30,35,40', c => '15-18,30,35,120' }; for my $key (keys %$test) { print "Processing '$key'\n"; $test->{$key} =~ s/(-?\d+)-(-?\d+)/join(",", ($1..$2))/eg; $test->{$key} = [split(',', $test->{$key} )]; } print Dumper($test);

    ___________
    Eric Hodges