Learned Monks,
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...
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
Output looks like this...
A: $VAR1 = {
'state' => [
's_name1'
]
};
C: $VAR1 = {
'instance' => {
'i_name' => {
'f_name' => undef
}
}
};
Given the data above, the final expected data should look like this...
C: $VAR1 = {
'instance' => {
'i_name' => {
'f_name' => {
'state' => [
's_name1
+'
]
}
}
}
};
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
+'
's_name2
+'
]
}
'transition' => [
't_n
+ame'
]
}
}
}
};
Re: Grouping an array of hashrefs by similar key values was helpful. Searching for "*" is a tricky thing to do!
Your help is always appreciated!
Update: added the "expected results"
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.