Further to toolic's reply:

if(exists($final->{$x[$i]})){ print "second\n"; ($final->{$x[$i]}, $f); } else{ print "first\n"; $final->{$x[$i]} = ($final->{$x[$i]}, $f); }
The statement
    ($final->{$x[$i]}, $f);
does nothing. What do you expect it to do?

The statement
    $final->{$x[$i]} = ($final->{$x[$i]}, $f);
is a bit more complex. It evaluates the list  ($final->{$x[$i]}, $f) in the scalar context imposed by the scalar  $final->{$x[$i]} on the LHS of the assignment. I doubt this statement does what you expect. Please see the Context tutorial, in particular the "Context clash" section.

c:\@Work\Perl>perl -wMstrict -MData::Dumper -le "my $final = { qw(a 1 b 2 c 3), d => [ 9, 8, 7 ] }; my @x = qw(a b c); print 'data to start'; print Dumper $final; print Dumper \@x; ;; my $f = 'ZOT'; ;; print 'void-context expression'; ($final->{$x[1]}, $f); print Dumper $final; ;; print 'assign list in scalar context'; $final->{$x[1]} = ($final->{$x[1]}, $f); print Dumper $final; " Useless use of hash element in void context at -e line 1. Useless use of hash element in void context at -e line 1. Useless use of private variable in void context at -e line 1. data to start $VAR1 = { 'c' => '3', 'a' => '1', 'b' => '2', 'd' => [ 9, 8, 7 ] }; $VAR1 = [ 'a', 'b', 'c' ]; void-context expression $VAR1 = { 'c' => '3', 'a' => '1', 'b' => '2', 'd' => [ 9, 8, 7 ] }; assign list in scalar context $VAR1 = { 'c' => '3', 'a' => '1', 'b' => 'ZOT', 'd' => [ 9, 8, 7 ] };
Please pay attention to the warning messages generated by warnings. (The "Useless use of..." messages will have useful line numbers associated with them if you run your code from a script rather than from the command line as I have.)

I think there may be other problems with the script, but this should get you going. Perhaps also see Perl Data Structures Cookbook.


Give a man a fish:  <%-(-(-(-<


In reply to Re: Ill-formed hash of hash - to convert text to xml by AnomalousMonk
in thread Ill-formed hash of hash - to convert text to xml by themonk

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.