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"


In reply to pointer/alias question by shoness

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.