Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Combinations of lists, etc

by LanX (Saint)
on Oct 04, 2019 at 18:50 UTC ( [id://11107064]=note: print w/replies, xml ) Need Help??


in reply to Combinations of lists to a hash

TIMTOWTDI

DB<41> x ($pre, $ranges) = split /=/, "Prefix1=A:b,c:1,2", 2 0 'Prefix1' 1 'A:b,c:1,2' DB<42> $del="=" DB<43> x @keys =$pre 0 'Prefix1' DB<44> for $mult (split /:/, $ranges ) { @keys = map { $a=$_; map { +"$a$del$_" } split /,/, $mult } @keys; $del = ':' } DB<45> x @keys 0 'Prefix1=A:b:1' 1 'Prefix1=A:b:2' 2 'Prefix1=A:c:1' 3 'Prefix1=A:c:2' DB<46> @hash{@keys} = ('value2') x @keys DB<47> x \%hash 0 HASH(0x3598330) 'Prefix1=A:b:1' => 'value2' 'Prefix1=A:b:2' => 'value2' 'Prefix1=A:c:1' => 'value2' 'Prefix1=A:c:2' => 'value2' DB<48>

HTH! :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Combinations of lists, etc
by tel2 (Pilgrim) on Oct 05, 2019 at 00:59 UTC
    Nice work, LanX, thank you!
    I tried that in a script, getting it to read all the lines of data, and it works perfectly!
    Here it is:
    #!/usr/bin/perl while (<DATA>) { #($pre, $ranges) = split /=/, $_, 2; ($pre, $ranges, $value) = $_ =~ /^(.+?)=([^ ]+) (.+)$/; $del = "="; @keys = $pre; for $mult (split /:/, $ranges) { @keys = map { $a=$_; map { "$a$del$_" } split /,/, $mu +lt } @keys; $del = ':'; } #@hash{@keys} = ('value2') x @keys; @hash{@keys} = ($value) x @keys; } use Data::Dump 'dd'; dd \%hash; __DATA__ Prefix1=A,B:c,d value1 Prefix2=A:b,c:1,2 value2 Prefix3=A:*:1,2 value3
    And the output:
    { "Prefix1=A:c" => "value1", "Prefix1=A:d" => "value1", "Prefix1=B:c" => "value1", "Prefix1=B:d" => "value1", "Prefix2=A:b:1" => "value2", "Prefix2=A:b:2" => "value2", "Prefix2=A:c:1" => "value2", "Prefix2=A:c:2" => "value2", "Prefix3=A:*:1" => "value3", "Prefix3=A:*:2" => "value3", }
    If you have any suggested changes to my changes, I'm all ears.
    I've also added 4 updates to my original post in case you're interested.  No worries if not. You can ignore update #1.
      That's not my code. You replied to the wrong person/post (and no glob in this one).

      No idea. Sorry.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Sorry LanX - I got mixed up.
        I've updated my last post now with your code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found