Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Updating A Hash Recursively

by QM (Parson)
on Sep 06, 2005 at 17:56 UTC ( #489624=note: print w/replies, xml ) Need Help??


in reply to Updating A Hash Recursively

glob to the rescue? glob can generate string combinations/permutations in many cases, depending on your needs.
#!/your/perl/here use strict; use warnings; my %line = ('A' =>1, 'B' =>1, 'C' =>1); # last combination to emit (based on grep filter) my $last = 'AYW'; # build up strings for use with glob() my @line_keys = sort keys %line; my @tojoin = sort ('', qw (W X Y Z)); my $line_keys = join ',', @line_keys; my $tojoin = join ',', @tojoin; # alternation in glob uses '{string1,string2,...}' my $glob = '{' . join( '}{', $line_keys, $tojoin, $tojoin ) . '}'; # globbing @glob = glob $glob; # only keep strings shorter than $last, # or le $last my @glob = grep length($_) < length($last) || $_ le $last, @glob; # populate hash (easier ways???) @line{@glob} = (1) x @glob; # print results (note sort sub) foreach my $key (sort string_length keys %line) { print "$key => $line{$key}\n"; } # sort by length, then lexicographically sub string_length { length($a) <=> length($b) or $a cmp $b }
yields
A => 1 B => 1 C => 1 AW => 1 AX => 1 AY => 1 AZ => 1 BW => 1 BX => 1 BY => 1 BZ => 1 CW => 1 CX => 1 CY => 1 CZ => 1 AWW => 1 AWX => 1 AWY => 1 AWZ => 1 AXW => 1 AXX => 1 AXY => 1 AXZ => 1 AYW => 1

-QM
--
Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://489624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2020-05-27 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (162 votes). Check out past polls.

    Notices?