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


Alright, so I have been reading the perl docs stuff online about references for most of the day.
Most of it is for much easier stuff than Marshal had written in his example. On lines where Marshal uses references to a hash of hashes of arrays (I think???), could someone explain them to me I am not getting it?

For Example:
push @{$recordState{$record_id}{$OwnerOrWaiting}}, $_; if ( @{$recordState{$recNum}{WAITING}} >= 1) foreach my $line ( @{$recordState{$recNum}{WAITING}} )


I know that, lets say:
$myArray = \@array; is the same as @{$myArray}, which is referring to the array "@array"
-OR-
$myHash = \%hash is the same as %{$myHash}, which refers to the hash "%hash", I think????

Also, that square brackets after a reference is referring to an element in that array. Like this, %{$myArray}[3] refers to the 4th element in the array it refers to.
And for a hash reference, keys %myHash is the same as, keys %{$myHash}.


Or what would be even more helpful, if you or anyone has some time, could you take Marshal's code and document it out to where a newbie could relate (i.e. comments before any of the more difficult lines)?

If this is too much trouble, I understand, it's alot of work. I mean I could just take the code and probably use it as is, but I really want to learn this stuff.

Extremely Thankful,
Matt

Replies are listed 'Best First'.
Re^5: How to reference to array keys?
by Marshall (Canon) on Jul 28, 2011 at 03:07 UTC
    Matt,
    I rewrote this with a different data structure, this is just a simple array of lines (an Array of references to lines).
    Study the info you have been given.
    What you want is relatively complicated to do. But I figure that you are motivated enough to get it done.

    This code dumps the basic data in a 1st report.
    The 2nd report shows the the folks who own a file and the other guys who want it. The report specifically excludes those who own it and want it themselves.

    So the data structure is easier to understand, but the price is that it is a bit more convoluted to fiddle with.

    I'm sure that the other kind Monks and I will be willing to help as you have other questions.

    #!/usr/bin/perl use strict; use warnings; use Data::Dump qw(pp); #Data::Dumper is also great! $|=1; #turn off STDOUT buffering my @records; my $OwnerOrWaiting=""; while (<DATA>) { next if /^\s*$/; # skip whitespace lines s/\s+$//; # delete all trailing whitespace # this does a chomp() and a bit more.. if (/^FILENAME/) # FILENAME line defines what col #4 means { $OwnerOrWaiting = (split)[3]; } if (m/^\//) # A data line starts with '/' # I don't like "leaning toothpick" but # ok if your text editor displays wrong # colors with the other syntax - no biggie { push @records, "$OwnerOrWaiting $_"; #add status to line } } sub get_fields { my $line = shift; my %fields; @fields{qw(OWNER-WAIT FILENAME RECORD_ID M USER UNBR UNO TTY TIME D +ATE)} = split(' ', $line, 10); return %fields; } print "Dumping raw records....\n"; foreach my $line (@records) { my ($colnumber1, @rest) = split(' ',$line); printf "%-8s @rest\n", $colnumber1; } # lets print lines where we have an OWNER and at least one # other person WAITING who is not the OWNER print "\n\n"; my $current_owner=""; my $first_record=0; my @lines_in_record=(); my %temp; foreach my $line (@records) { %temp = get_fields($line); #make hash of field names if ( $temp{'OWNER-WAIT'} eq 'OWNER') # OWNER LINE { process_owner_record(); } else # WAITING LINE { push @lines_in_record, $line; } } process_owner_record(); sub process_owner_record { if (!$current_owner) { $current_owner = $temp{USER}; return; } my $new_owner = $temp{'USER'}; foreach (@lines_in_record) { my %this_record= get_fields($_); print "owner=$current_owner $_\n" if $current_owner ne $this_record{USER}; } @lines_in_record = (); $current_owner = $new_owner; } =prints Dumping raw records.... OWNER /prod-data/J 00151120273 X jmorg 3584038 247 ts/49 13:38:12 J +ul 20 WAITING /prod-data/J 00151120273 X jmorg 2015244 134 s/109 13:48:32 J +ul 20 WAITING /prod-data/J 00151120273 X gdavi 1359996 62 ts/20 13:54:22 Ju +l 20 OWNER /prod-data/J 001!L!311895 X jmorg 2015244 134 s/109 13:48:32 +Jul 20 WAITING /prod-data/J 001!L!311895 X jmorg 5713932 191 ts/46 14:01:42 +Jul 20 OWNER /prod-datahi 001!10274882 X rfuse 3354796 61 ts/43 13:39:02 J +ul 20 WAITING /prod-datahi 001!10274882 X jmorg 3584038 247 ts/49 13:39:22 +Jul 20 owner=jmorg WAITING /prod-data/J 00151120273 X gdavi 1359996 62 + ts/20 13:54:22 Jul 20 owner=rfuse WAITING /prod-datahi 001!10274882 X jmorg 3584038 24 +7 ts/49 13:39:22 Jul 20 =cut