Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Reading Install type config file into hash

by tmurnane (Novice)
on Aug 06, 2012 at 20:56 UTC ( #985834=perlquestion: print w/ replies, xml ) Need Help??
tmurnane has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to read a config file that will have something like the following information:

Product1 property=value property2=value Product2 feature1 property=value property2=value feature2 property3=value Product3 feature1 property=value property2=value

I need to read this file in and display it in a Perl Tk project. I'm just having a hard time figuring out how to keep track of the features because not all products have features.

@file = <IN>; foreach(@file){ if (/^TSS/){ # All product begin with TSS # Its a product }elseif ($_ =~ /=/){ # get the property and value using split }else{ # Its a feature??? } } # Should I use something like: $hash{$product}{$feature}{$property} = "value"; # or $hash{$product}{$property} = "value"; $hash{$product}{feature} = "feature name"; # if the feature key is her +e, there is a feature. # but how do I keep properties listed after the correct feature!

Obviously some sort of Hash should do the trick but having hard time visualizing it. Any help is appreciated.

Comment on Reading Install type config file into hash
Select or Download Code
Re: Reading Install type config file into hash
by BrowserUk (Pope) on Aug 06, 2012 at 21:05 UTC

    It depends a lot on how your code will process the resultant hash, but I'd consider adding a 'default' feature to those products that do not have features, just to make the code/hash structure consistent.

    You can always exclude it (the default feature) from displays, when it is the only feature.


    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?

      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.

        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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985834]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-09-01 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls