Hi, I've never used perl with XML. Can you please help me with the following questions.Also if you can give me any good web sites for perl with XML (basic concepts).
use strict;
my $sampleXML = qq(
<?xml version="1.0"?>
<response>
<portfolio>
<portID>Test</portID>
<portname>Consors Test Portfolio</portname>
<xloss>-5124.88530628508</xloss>
<riskgrade>129.39826419201</riskgrade>
<value>114260.469443242</value>
<divriskgrade>68.4645462588067<divriskgrade>
<divxloss>-2131.56017715564</divxloss>
<position>
<posID>Bayer</posID>
<xloss>-197.254957013363</xloss>
<riskgrade>148.955450569846</riskgrade>
<value>6138</value>
<riskimpact>-0.00822567904632093</riskimpact>
</position>
<position>
<posID>Bond</posID>
<xloss>-157.580177492695</xloss>
<riskgrade>29.3498701802706</riskgrade>
<value>18922.4694432424</value>
</position>
</portfolio>
</response>
);
$portresults = parsePortfolioResponse($sampleXML);
sub parsePortfolioResponse {
my $response = $_[0];
my %portresults;
my $stats = "riskgrade|riskimpact|xloss|value|divriskgrade|div
+xloss|posID";
#assumes portstats come first
my $ref = "portfolio";
while ($response =~ m!<($stats)>(.*?)</($stats)>!g) {
if ($1 eq "posID") {
$ref = $2;
} else {
$portresults{$ref}->{$1} = $2 if $2;
}
}
return \%portresults;
}
###END SAMPLE CODE
Questions:
1. Why do we pass the response to the function rather than just using it (under mod_perl)?
2. Why is the XML in $sampleXML badly formed (hint: check the tags)? How will the contents of $portresults differ from the desired results because of this?
3. What is the value of "$portresults->{portfolio}->{riskgrade}"?
3. What is the value of "sprintf("%.2f", -$portresults->{Bayer}->{xloss} )"?
4. What is the first element of "sort {lc($b) cmp lc($a)} keys %{$portresults}" ?
5. Given an HTML page, what would "
$HTML =~ s/<(?!\/*[bi]>).*?>/x/g;" do?
Edit Masem 2001-10-25 - Code tag on last question
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.