Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Ill-formed hash of hash - to convert text to xml

by AnomalousMonk (Archbishop)
on Jul 20, 2015 at 23:15 UTC ( [id://1135504]=note: print w/replies, xml ) Need Help??


in reply to Ill-formed hash of hash - to convert text to xml

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:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1135504]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found