in reply to Re^2: How to reference to array keys?
in thread How to reference to array keys?

Hey Matt, extra post is no problem at all.
If I am thinking correctly it just checks for line that start with a "/". Right? Yes, Right!
I see you have a link to some more info on references, I'll hit a few highlights specific to this code.

%recordState is a hash with basically 2 keys. In a complex data structure, everything is a reference until you get to the very last dimension where the data is. The value of $recordState{$record_id} is a reference to a another hash. The key to that hash is $OwnwerOrWaiting which is either set to "OWNER" or "WAITING" and the value is a reference to an array of input lines. Look at the output of Data::Dump and see the "key => value" pairs.

Also take a look at my dump loop, foreach my $recHashref ( values %recordState). There I didn't even use the $record_id key at all! I just get the values of the first dimension hash, where are references to the 2nd dimension hash. In the line
print "WAITING $_\n",  for @{$recHashref->{WAITING}}; $recHashref is dereferenced and the WAITING key is accessed. The values of that key is a reference to an array of lines. I say that that whole thing, $recHashref->{WAITING} is an array reference that I want to dereference by enclosing it in another set of curly braces and putting an @ in front. Each line gets printed out. Note that I used the exact same code pattern for OWNER as for WAITING. There is only one OWNER and that could have been:
print "OWNER   $recHashref->{OWNER}[0]\n"; which would mean give me the 0th line in the array pointed to by $recHashref->{OWNER}. There is a certain value in uniformity and so I did not handle the case of just one OWNER separately.

Maybe I confused you even more, but it this was remotely understandable, then the answer to you question above is that value of $recordState{$record_id}{$OwnerOrWaiting} is a reference to an array, which is dereferenced with the @ so that the current line can be pushed onto it.

It could very well be that the data structure can be simplified once I read more about what you are needing as an end result.

Replies are listed 'Best First'.
Re^4: How to reference to array keys?
by mmartin (Monk) on Jul 28, 2011 at 13:34 UTC
    Hey Marshal,

    Thank you very much for taking the time to explain things to me. I will start going over all the info you gave me, and I will reply back in a little while after I gain some headway with this.


    Thanks, Matt

Re^4: How to reference to array keys?
by mmartin (Monk) on Jul 28, 2011 at 19:39 UTC

    Hey Marshal,

    I've defiantly made some headway with the new simplified version that you gave me. But I just have one questions about it at the moment.

    1. For the section in the code where it Dumps the raw records,
    foreach my $line (@records) { (my $colnumber1, @rest) = split(' ',$line); printf "%-8s @rest\n", $colnumber1; }
    how does the first line in the loop (one with the 'split' function) work?
    I get that your creating and assigning 2 new variables, but how does it work setting one split function equal to 2 different variables (and 2 different types of variables)? By doing 2 print statements inside that loop, one fore each variable, I can see that:
    $colnumber1 = either 'OWNER' -or- 'WAITING'
    @rest = one whole line of data not including 'OWNER' or 'WAITING'


    UPDATE
    Came across one other question for the moment.
    In the same file what does the line if (!$current_owner) check for if $current_owner has no value? Or does it?


    Thanks,
    Matt


      The line with split works this way: First of all the left side of the equation is in parentheses, so it provides list context to the left side (many functions return different things in different contexts, see the first two paragraphs of split

      So the split in list context returns a list of strings that get distributed to the list on the left side of the equation. This list begins with a scalar, so the first item in the list is stored in the scalar. The second variable is an array and arrays just take everything they can get, so all the other strings are pushed into @rest. If there were another array or scalar at the end of the list on the left side, it would be empty. If another scalar were before @rest, it would hold the second item of the list of strings

      (!$current_owner) is true when $current_owner is false. $current_owner is false if it is undef, the empty string ("") or 0. Since $current_owner is initialized with the empty string, this condition is at least true the first time the subroutine process_owner_record is called.


        Jethro,

        Thanks for the reply. Good explanation, makes much more sense now!


        Thanks, Matt