in reply to Re: hash with multiple values per key
in thread hash with multiple values per key
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);
|
|---|