shoness has asked for the wisdom of the Perl Monks concerning the following question:
I build a data structure as I parse a file. The gist of it is in the code below. I am using Perl 5.8.5 if that matters.
I go round and round. Sometimes I read information on one line and that sets the "context" for the data on the lines that immediately follow. When I want to actually store something, I'm left with a number of state variables to index to where I need to put the data. Instead, I hope to use this "context" to just set a pointer into the data structure that I assemble and keep this as one state variable.
I then hope to collect the data into an anonymous array aliased by the hashref pointer.
Everything looks okay at first. My first call to "Dumper" prints what I expect. When I get to the end however, I see that the data was never placed into the main data structure. If I make multiple passes that should "push" to the same array, they don't show up either.
My pointer/alias is not working...
Output looks like this...use strict; use warnings; use Data::Dumper; my $data; my $fsm = 1; my $m = ''; my $i = ''; while(<DATA>) { our $pointer; local *pointer; chomp; if (/^INSTANCE:\s+(\S+)/) { $m = ''; $i = $1; } elsif (/^MODULE:\s+(\S+)/) { $m = $1; $i = ''; } elsif (/^Fsm\s+(\S+)/) { next if !$fsm; if ($m) { *pointer = \$data->{'module'}->{$m}->{$1}; } else { *pointer = \$data->{'instance'}->{$i}->{$1}; } } elsif (/^State\s+(\S+)/) { next if !$fsm; push(@{ $pointer->{'state'} }, "$1"); print "A: " . Dumper( $pointer ); #DEBUG } elsif (/^Transition\s+(\S+)/) { next if !$fsm; push(@{ $pointer->{'transition'} }, "$1"); print "B: " . Dumper( $pointer ); #DEBUG } } print "C: " . Dumper($data); __DATA__ INSTANCE: i_name Fsm f_name State s_name1 # Transition t_name # State s_name2
Given the data above, the final expected data should look like this...A: $VAR1 = { 'state' => [ 's_name1' ] }; C: $VAR1 = { 'instance' => { 'i_name' => { 'f_name' => undef } } };
If you uncomment the commented lines in the __DATA__ section, then I'd expect data like this...C: $VAR1 = { 'instance' => { 'i_name' => { 'f_name' => { 'state' => [ 's_name1 +' ] } } } };
Re: Grouping an array of hashrefs by similar key values was helpful. Searching for "*" is a tricky thing to do!C: $VAR1 = { 'instance' => { 'i_name' => { 'f_name' => { 'state' => [ 's_name1 +' 's_name2 +' ] } 'transition' => [ 't_n +ame' ] } } } };
Your help is always appreciated!
Update: added the "expected results"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: pointer/alias question
by GrandFather (Saint) on Dec 02, 2010 at 00:38 UTC | |
by shoness (Friar) on Dec 03, 2010 at 01:43 UTC | |
by GrandFather (Saint) on Dec 03, 2010 at 02:53 UTC | |
by shoness (Friar) on Dec 03, 2010 at 20:33 UTC | |
by toolic (Bishop) on Dec 03, 2010 at 01:52 UTC | |
Re: pointer/alias question
by toolic (Bishop) on Dec 01, 2010 at 19:56 UTC | |
by shoness (Friar) on Dec 01, 2010 at 20:35 UTC | |
Re: pointer/alias question
by Anonymous Monk on Dec 02, 2010 at 00:27 UTC |