On Sat, Feb 16, 2002 at 11:20:42AM -0500, Ian P. Thomas wrote:
> If I read this correctly, I takes the first array from
> unchecked_dfa_states and passes it out, but not before dereferencing
> it. I needed something that could take the first array as a whole
> object and pass it to checked_dfa_states, another array of arrays.
> After pouring through my Perl books, I think what I needed to do was to
> give each stored array in unchecked_dfa_states a scalar reference.
> This way I could pass the reference to the whole array. Does that
> sound right?
It sounds like you have a fundamental misunderstanding about
multi-dimensional arrays in Perl. A multi-dimensional array in Perl is just
an array of array references; these references are scalar values, and can be
passed around as single entities.
So the code:
@checked_dfa_states = @{ shift(@unchecked_dfa_states) };
Removes the first element from @unchecked_dfa_states, dereferences it, then
assignes it to @checked_dfa_states. So, given:
@checked_dfa_states = ();
@unchecked_dfa_states = (
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
);
After the code I showed runs you'd have:
@checked_dfa_states = (1, 2, 3);
@unchecked_dfa_states = (
[4, 5, 6],
[7, 8, 9],
);
If, instead, you wanted, as I suspect from your question, the result:
@checked_dfa_states = (
[1, 2, 3],
);
@unchecked_dfa_states = (
[4, 5, 6],
[7, 8, 9],
);
Then don't dereference the shift; in other words, use:
@checked_dfa_states = shift(@unchecked_dfa_states);
Does that answer your question?
> The alternative that I used was keeping two arrays. One just
> held information about the size of the arrays in unchecked_dfa_states.
> I called it unchecked_dfa_states_sizes. I did a for loop using a
> value from the unchecked_dfa_states_sizes array as the ceiling to put
> each element into a temporary array. I then pushed this temporary
> array onto checked_dfa_states. The reason I was able to do this is
> because I kept the two arrays,
>
> unchecked_dfa_states
> unchecked_dfa_states_sizes
>
> in sync. That meant that every subscript for
> unchecked_dfa_states_sizes matched a corresponding subscript for an
> array in unchecked_dfa_states. A reference to an array, and a separate
> reference to its size.
Just to be clear, your two arrays would look something like this:
@unchecked_dfa_states = (
[1, 2, 3],
[4, 5, 6, 7],
[8, 9],
);
@unchecked_dfa_states_sizes = (
3,
4,
2,
);
where the size of $unchecked_dfa_states[$n] is in
$unchecked_dfa_states_sizes[$n].
This is unnecessary in Perl. @{$unchecked_dfa_states[$n]} in scalar context
would yield the size of $unchecked_dfa_states[$n].
Instead of iterating over elements, like you describe, it's a fairly simple
matter to copy a dimension from @unchecked_dfa_states to
@checked_dfa_states:
$checked_dfa_states[$n] = [ @{$unchecked_dfa_states[$n]} ]
This is much like the altered shift operation I showed you:
@checked_dfa_states = shift(@unchecked_dfa_states);]
Except that it's not destructive of either @checked_dfa_states or
@unchecked_dfa_states.
> I believe you want:
>
> push(
> @unchecked_dfa_states,
> dfa_state_creation($unchecked_dfa_states[0],
> $symbol)
> );
>
> I can't remember exactly, but I believe when I tried to do it
> this way, it passed an actual numeric value, the memory address to the
> array referenced by $unchecked_dfa_states[0]. As above, I stored the
> array referenced by $unchecked_dfa_states[0] into a temporary array and
> passed that in like this
The first argument to dfa_state_creation() call would be a reference. In
numeric context this is a very large number. However, this reference can be
dereferenced and used as if you were dealing directly with
$unchecked_dfa_states[0], because you actually are.
> This worked. Thanks for the help. I think I need to spend
> some more time with the animal books and reading this list. I ended up
> having some problems using global variables later in the program. Next
> time I'll cut and paste the code. :)
Yes, reviewing the books is a very good idea. You should also read perldoc
perlreftut and perldoc perlol. The first is a tutorial on references, the
second documents how to use lists of lists, or multi-dimensional arrays.
There's also perldoc perlref and perldoc perldsc, describing references and
data structures, respectively.
Also, keep in mind that when multi-dimensional arrays are talked about in
Perl they're not quite the same thing as in other languages. Some languages
are fixed in their dimensions: for instance, in C you can have a 3x2 array,
or 3x3x3, etc. With Perl each of the elements of an array can be any
length: a prime example is from above:
@unchecked_dfa_states = (
[1, 2, 3],
[4, 5, 6, 7],
[8, 9],
);
The toplevel array has 3 elements, but each sub-array has a variable number
of elements.
I'm not sure if this matters in what you're doing, but it's something to
keep in mind in case it affects you.
I hope that helps a little more. Good luck.
Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com
--
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]