<snip>

> > 
> > Though the use of C<$current> could be avoided if you could read
> > multiple lines at a time, if that is possible I would switch to that
> > method. If not the above should do.
> > 
> > If you are confused or this doesn't work, give it another shot and
> > provide at least 10 entries of data.

>  You hit it on the Spot !!! I am begining to understand where I was
going wrong. I do have anotehr Q....Can you exaplain why this is correct
scope, and why here unlike in my code Vendor is not recreated ??
> 
>  $rating->{$fields[4]}->{$fields[2]} = $current = {};
> 

Normally the scope of C<$current> shouldn't need to be outside of the
foreach, but in this case since the following lines need to be added to
the same hash we are currently working on, but we will no longer have
access to C<$fields[4]> and C<$fields[2]>, we have to have some way to
access that value (a reference) of the "middle" hash.  So rather than
keeping track of two temp variables, namely which vendor and type we are
acting on, I just keep track of the current reference and manipulate it
instead.  Either way would work, but to follow better (read: stricter)
scoping rules you would have to be able to read the 3 lines of input at
once, which you could do pretty easily, especially with a C<while> loop
instead where you advance the iterator by 2 when you slurp in the lines,
but you didn't go that direction so I avoided it too.

Your code was continually clobbering the value in the Vendor code, and
was setting the type as one of the values of the 2nd level hash. Mine
sets the value of the Vendor code to a hash, but then sets within that
hash (the middle or 2nd level) the type code to a value to a 3rd level
hash which is where the quantity (which was misspelled before by the
way) and color then get set. So you end up with a list of all properties
(qty and color) for each type for each vendor. Obviously I am doing a
pretty poor job of describing it :-)... seriously check out the data
structure/reference docs I mentioned before, they should be able to
clear things up much better than I.

perldoc perldsc
perldoc perllol
perldoc perlreftut
perldoc perlref

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to