Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

A few days ago I asked a question here about how to use an XML spellcheck service and a gentleman here show me a sample code using SOAP::WDSL and it works great. Please see: http://www.perlmonks.org/?node_id=651558 . The data I got back from the SOAP service is in this format:
$VAR1 = { 'body' => 'thi is a test', 'ver' => '1.0', 'MisspelledWordCount' => '1', 'MisspelledWord' => { 'SuggestionCount' => '25', 'word' => 'thi', 'Suggestions' => [ 'ti', 't hi', 'the', 'phi', 'tho', 'thy', 'chi', 'th', 'this', 'thin', 'hi', 'thing', 'thaw', 'Thai', 'their', 'then', 'thee', 'thou', 'thew', 'they', 'thigh', 'Thia', 'thaws', 'Thu', 'theirs' ] } };
so I assigned it to a hash variable %result = Dumper($som->paramsall); but I can not acess this data using print "$result{'MisspelledWordCount'}";
so my question really is how do I access these data without manually using the pattern matching technique such as $result = /MisspelledWordCount/ ?

Replies are listed 'Best First'.
Re: Help With Parsing XML format / Hash of Hashes
by GrandFather (Saint) on Nov 23, 2007 at 23:52 UTC

    If what you did was as shown then you should have received a heads up along the lines of: Odd number of elements in hash assignment at .... You do use strictures (use strict; use warnings;) don't you?

    What was returned would have been a hash reference which you need to assign to a scalar:

    my $result = subReturningHashRef ();

    which you then access fields of by:

    print "Found $result->{MisspelledWordCount} miss-spelled words\n";

    Perl is environmentally friendly - it saves trees
      Thanks for your response. I got an error when I put in the line my $result = subReturningHashRef();
      Undefined subroutine &main::subReturningHashRef called
      Here is my complete code:
      #!/usr/bin/perl use HTML::Template; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use SOAP::WSDL; use Data::Dumper; # config vars my $tocheck = param('text'); #my $license = '12345yourlicensekey'; my $service = 'http://ws.cdyne.com/SpellChecker/check.asmx'; my $wsdl = "$service?WSDL"; # initialise the SOAP proxy object my $soap = SOAP::WSDL->new(); $soap->wsdl($wsdl); $soap->on_action(sub { return $_[0].$_[1]; }); # uncomment if you get +a soapfault containing something like 'invalid method specification'. $soap->proxy($service); $soap->wsdlinit(caching => 1) || die "couldn't connect to soap service!"; # call the CheckTextBody method exposed by the web-service my $som = $soap->CheckTextBody( BodyText => $tocheck, LicenseKey => $license); # examine the result returned by the web-service print "Content-type: text/html\n\n"; print "<html>"; print "<head>"; print "<title>OS2 - Lab3 Project</title>"; print "</head>"; print "<body>"; print qq{<center><b><font size="+3">Online Spellchecker via SOAP and X +ML</b></center>}; print "<br>"; print qq{<form method="post" action="lab3.cgi">}; print "<table>"; print qq{<tr><td>Enter Text:</td><td>Word:</td></tr>}; print qq{<tr><td rowspan="12" valign="top"><textarea cols="45" rows="1 +2" name="text">$tocheck</textarea>}; print qq{</td><td><input type="text" name="word" value=""></td></tr>}; print qq{<tr><td>Suggestions:</td></tr>}; print qq{<tr><td rowspan="8"><select size="10">}; print qq{<option value =" ">---------------End---------------</option> +}; print qq{</select></td>}; print qq{</tr>}; print qq{<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></t +r><tr></tr><tr></tr></tr><tr></tr>}; print qq{<tr><td><input type="submit" name="submit" value="Submit">}; print qq{</td><td><input type="submit" name="Replace" value="Replace"> +</td></tr>}; print "</table>"; print "<br>"; print "<br>"; print "<b>XML Data Received:</b>"; print "<br>"; if ($som->fault) { $result = $som->faultstring; print qq{<textarea cols="55" rows="10" name="response">$result</textar +ea>}; } else { $result = Dumper($som->paramsall); #%result1 = $som->paramsall; #%result1 = $result; #$value = $result1{'body'}; $result = subReturningHashRef (); print qq{<textarea cols="45" rows="10" name="response">$result->{Missp +elledWordCount}</textarea>}; } print "<br>"; print "</form>"; print "</body>"; print "</html>";
Re: Help With Parsing XML format / Hash of Hashes
by Anonymous Monk on Nov 24, 2007 at 03:22 UTC
    OK, I played with it all day and seems to get some of the data out of those hashes by assigning variables as this:
    $result = $som->paramsall; $result1 = $result->{'MisspelledWord'}; $nosuggestions = $result1->{'SuggestionCount'}; $mistake = $result1->{'word'}; $suggestion = $result1->{'Suggestions'};
    I am unable to get to the $result1->{'Suggestions'} since there are so many values in it. Could someone please show me how to access this structure. Thanks.
      e.g., to access the second element of the array of suggestions, use

      my $second_suggestion = $result1->{'Suggestions'}[1];

      to get a copy of the entire array of suggestions, use

      my @suggestions = @{ $result1->{'Suggestions'} };

      see the Perl Data Structures Cookbook perldsc for more info and related links.