in reply to Matching String Value with Hash Key

G'day Mark.Allan,

"Does this make sense? it was hard for me to explain never mind write"

To be honest, I'm not entirely sure: hopefully, I got the gist of it. Here's some issues I found:

Here's a script that captures the data you want. I'll leave you to format the output.

#!/usr/bin/env perl use strict; use warnings; use autodie; my $config_file = 'pm_1056046_parse_config.txt'; my $gen = 'parent'; my %cfg; { local $/ = "DEPLOYED\n"; open my $config_fh, '<', $config_file; while (<$config_fh>) { $gen = 'child' if /CHILD CONFIG/; /(\w+)\s+HOLDS\s+(\w+)/; $cfg{$gen}{$1}{holds} = $2 if $gen eq 'child'; push @{$cfg{parent}{$1}{equals}} => /(total_\w+)/g if $gen eq +'parent'; } close $config_fh; } for (keys %{$cfg{child}}) { print "$_ : @{$cfg{parent}{$cfg{child}{$_}{holds}}{equals}}\n"; }

Output:

$ pm_1056046_parse_config.pl first_distribution : total_finish total_cancel second_distribution : total_cancel third_distribution : total_finish total_cancel forth_distribution : total_dist total_finish total_cancel

-- Ken

Replies are listed 'Best First'.
Re^2: Matching String Value with Hash Key
by Mark.Allan (Sexton) on Sep 29, 2013 at 10:22 UTC

    Ken

    Thanks for your input, I'll pick up on this tomorrow but one thing I failed to highlight is that ###parent config#### and #####child config##### don't actually exist in the file, it's was merely me defining the structure which I forgot to mention

    The exact file looks likes this

    DEPLOY : SWG_Lower HOLDS SWG_Depot EQUALS:= total_dist: completion= SUCCESS total_finish: completion= SUCCESS total_cancel:completion=SUCCESS DEPLOYED DEPLOY: LOADED_Lower HOLDS SWG_Depot EQUALS:= total_finish: completion= SUCCESS total_cancel: completion=SUCCESS DEPLOYED DEPLOY: CONFIG_Lower HOLDS SWG_Depot EQUALS:= total_cancel: completion=SUCCESS DEPLOYED DEPLOY first_distribution HOLDS LOADED_Lower DEPLOYED DEPLOY: second_distribution HOLDS CONFIG_Lower ; DEPLOYED DEPLOY : third_distribution HOLDS LOADED_Lower; DEPLOYED DEPLOY : forth_distribution HOLDS SWG_Lower ; DEPLOYED
      "... one thing I failed to highlight is that ###parent config#### and #####child config##### don't actually exist in the file, it's was merely me defining the structure which I forgot to mention"

      You actually wrote:

      "I have the following text file of configuration details."

      It's important to pay attention to details. I can only provide advice on dealing with the input you describe: I can't guess that some of what you provide is correct and other parts are not. Furthermore, the computer can only deal with the input (as you describe it) in your code; for instance, any attempt to match either version of the input with the pattern '#parent config#' would fail.

      You may think I'm being pedantic; however, if you look back to the issues I originally raised, you'll see that all six of them are related to a lack of attention to detail.

      For your latest version of your input, you can modify my code by changing

      $gen = 'child' if /CHILD CONFIG/;

      to

      $gen = 'child' unless /EQUALS/;

      and get the same output values.

      -- Ken

        ken, apologies for the sloppy post but its sorted now and your assistance was invaluable as ever

        thanks again,