Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Reading Install type config file into hash

by tmurnane (Novice)
on Aug 08, 2012 at 15:34 UTC ( #986312=note: print w/replies, xml ) Need Help??


in reply to Re: Reading Install type config file into hash
in thread Reading Install type config file into hash

The following code works fine.

sub readSetup { open IN, "<setup.ini" or die "cannot open setup.ini\n"; @setupFile = <IN>; close IN; foreach (@setupFile) { chomp; if (/^PROD/) { $Solution = $_; $Feature = 'none'; # Need to keep ordered list for viewing in UI push @Solutions, $Solution; } elsif ( $_ =~ /=/ ) { s/\s*=\s*/=/; ($property, $value) = split /=/, $_; $Installs{$Solution}{$Feature}{$property} = "$value"; } else { $Feature = "$_"; } } } sub print_it { for $Solution (@Solutions) { print "$Solution\n"; for $Feature ( keys %{ $Installs{$Solution} }) { $tab = "\t"; if ($Feature ne "none"){ print "$tab$Feature\n"; } else { $tab = "\t\t"; } for my $property ( keys %{ $Installs{$Solution}{$Feature} }) +{ print "$tab$property = $Installs{$Solution}{$Feature}{$prop +erty}\n"; } } print "\n"; } }

Is there a way of displaying it in the order it came in without using Tie::IxHash module. That is not on my system and will have to put in a ticket to have the module installed. My fear is that users of this script may not have it installed as well and thus would have to got through the same issue.

Running the above code on this file:

PRODUCT1 FEATURE1_PRODUCT1 PROPERTY1_FEATURE1_PRODUCT1=VALUE1 PROPERTY2_FEATURE1_PRODUCT1=VALUE2 PROPERTY3_FEATURE1_PRODUCT1=VALUE3 PRODUCT2 PROPERTY1_PRODUCT2=VALUE1 PROPERTY2_PRODUCT2=VALUE2 PRODUCT3 FEATURE1_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 PROPERTY2_FEATURE1_PRODUCT3=VALUE2 PROPERTY3_FEATURE1_PRODUCT3=VALUE3 FEATURE2_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 FEATURE3_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 PROPERTY2_FEATURE1_PRODUCT3=VALUE2 PROPERTY3_FEATURE1_PRODUCT3=VALUE3 PRODUCT4 PROPERTY1_PRODUCT4=VALUE1 PROPERTY2_PRODUCT4=VALUE2 PRODUCT5

produces this result:

PRODUCT1 FEATURE1_PRODUCT1 PROPERTY2_FEATURE1_PRODUCT1 = VALUE2 PROPERTY1_FEATURE1_PRODUCT1 = VALUE1 PROPERTY3_FEATURE1_PRODUCT1 = VALUE3 PRODUCT2 PROPERTY1_PRODUCT2 = VALUE1 PROPERTY2_PRODUCT2 = VALUE2 PRODUCT3 FEATURE2_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3 = VALUE1 FEATURE1_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3 = VALUE1 PROPERTY3_FEATURE1_PRODUCT3 = VALUE3 PROPERTY2_FEATURE1_PRODUCT3 = VALUE2 FEATURE3_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3 = VALUE1 PROPERTY3_FEATURE1_PRODUCT3 = VALUE3 PROPERTY2_FEATURE1_PRODUCT3 = VALUE2 PRODUCT4 PROPERTY2_PRODUCT4 = VALUE2 PROPERTY1_PRODUCT4 = VALUE1 PRODUCT5

As you can see - the results are unordered.

Replies are listed 'Best First'.
Re^3: Reading Install type config file into hash
by BrowserUk (Pope) on Aug 08, 2012 at 17:19 UTC
    Is there a way of displaying it in the order it came in without using Tie::IxHash module. That is not on my system and will have to put in a ticket to have the module installed.

    Do you have to have a ticket to install your code on your machine?

    Copy & paste the code into your own source file; and tell the controlling powers that you've done so.

    As you can see - the results are unordered.

    That is the nature of hashes. The use-cases for hashes don't require (or benefit) from them being ordered. Humans like things ordered, but programs don't give a damn.

    And if all you want to do is present the same information back to the users in the same form, just print the file to their screen.

    But if you really must remember the order, just make a note of it as you go:

    #! perl -slw use strict; my %ini; my( $product, $feature ); while( <DATA> ) { chomp; if( /\s+(\S+)=(\S+)/ ) { $ini{ $product // die "Malformed ini at:$." } { $feature // 'none' } { $1 } = $2; push @{ $ini{ $product }{ $feature // 'none' }{ order } }, $1; } elsif( /^PROD/ ) { push @{ $ini{ $product }{ order } }, 'none' if $product and not exists $ini{ $product }{ order }; $product = $_; push @{ $ini{ order } }, $product; undef $feature; } else { /^\s+(\S+)/ and $feature = $1; push @{ $ini{ $product }{order} }, $feature; } } for my $product ( @{ $ini{ order } } ) { print $product; for my $feature ( @{ $ini{ $product }{ order } } ) { print "\t", $feature if $feature ne 'none'; for my $type ( @{ $ini{ $product }{ $feature }{ order } } ) { print "\t\t$type=$ini{ $product }{ $feature }{ $type }"; } } } __DATA__ PRODUCT1 FEATURE1_PRODUCT1 PROPERTY1_FEATURE1_PRODUCT1=VALUE1 PROPERTY2_FEATURE1_PRODUCT1=VALUE2 PROPERTY3_FEATURE1_PRODUCT1=VALUE3 PRODUCT2 PROPERTY1_PRODUCT2=VALUE1 PROPERTY2_PRODUCT2=VALUE2 PRODUCT3 FEATURE1_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 PROPERTY2_FEATURE1_PRODUCT3=VALUE2 PROPERTY3_FEATURE1_PRODUCT3=VALUE3 FEATURE2_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 FEATURE3_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 PROPERTY2_FEATURE1_PRODUCT3=VALUE2 PROPERTY3_FEATURE1_PRODUCT3=VALUE3 PRODUCT4 PROPERTY1_PRODUCT4=VALUE1 PROPERTY2_PRODUCT4=VALUE2 PRODUCT5

    Produces:

    [18:17:59.55] C:\test>junk44 PRODUCT1 FEATURE1_PRODUCT1 PROPERTY1_FEATURE1_PRODUCT1=VALUE1 PROPERTY2_FEATURE1_PRODUCT1=VALUE2 PROPERTY3_FEATURE1_PRODUCT1=VALUE3 PRODUCT2 PROPERTY1_PRODUCT2=VALUE1 PROPERTY2_PRODUCT2=VALUE2 PRODUCT3 FEATURE1_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 PROPERTY2_FEATURE1_PRODUCT3=VALUE2 PROPERTY3_FEATURE1_PRODUCT3=VALUE3 FEATURE2_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 FEATURE3_PRODUCT3 PROPERTY1_FEATURE1_PRODUCT3=VALUE1 PROPERTY2_FEATURE1_PRODUCT3=VALUE2 PROPERTY3_FEATURE1_PRODUCT3=VALUE3 PRODUCT4 PROPERTY1_PRODUCT4=VALUE1 PROPERTY2_PRODUCT4=VALUE2 PRODUCT5

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Thank you so much - that is exactly what I am looking for.

      I do need it ordered because I'm creating a Perl Tk UI that will read this setup file, allow users to modify it and write it back out...basically, creating a Suite installer outside of InstallShield. I'm thinking of using a NoteBook where the first tab will have a list of Products and Features with CheckBoxes. The other tabs will have the "Property = Value" info in a label Textbox fashion so they can change the values and write out a new setup.ini.

      I will also have another script that can be run from the UI and cmdline for users without UI capabilities, that will just read the setup.ini, build the commands to run each install silently and run them...where obviously, order does not matter.

      Still have to figure that one out - when running outside the UI, how do I determine what to install. I May have to add a Product="y|n", Feature="y|n" to the setup.ini file.

      Thanks a bunch - I knew to use Arrays of Hashes but couldn't conceptualize it visually.

      Boy do I have a lot to learn - who'd thunk to use an || in a Hash Key. Great stuff.

      If anyone has a better solution...by all means, let me know.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986312]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2018-01-22 16:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (235 votes). Check out past polls.

    Notices?