Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

How many times do we have to tell the children? Do not hand roll code to parse HTML/XML, life just ain't long enough for that to be worth while - even as an exercise.

Use HTML::TreeBuilder for HTML or XML::Twig for XML. In this case it looks like XML so lets wrap a root element around the sample data provided and see what we can do:

use strict; use warnings; use XML::Twig; my $xml = <<XML; <root>
<act>Key</act><emp>3384</emp><job>78082</job><chap>6</chap><pg>20</pg> +<time>0.7</time><prod>114.285714285714</prod> <act>Reconcile</act><emp>3017</emp><job>78062</job><chap>2-7</chap><pg +>0</pg><time>1.4</time><prod>Insufficient Information</prod> <act>Training</act><emp>3384</emp><job>77654</job><chap>-</chap><pg>0< +/pg><time>5.1</time><prod>Non-Billable</prod> <act>Management</act><emp>3017</emp><job>77893</job><chap>-</chap><pg> +0</pg><time>4.4</time><prod>Non-Billable</prod> <act>Break</act><emp>3379</emp><job>33843</job><chap>-</chap><pg>0</pg +><time>0.2</time><prod>Non-Billable</prod> <act>Excess overload</act><emp>3379</emp><job>77570</job><chap>14</cha +p><pg>1</pg><time>0.5</time><prod>6.66666666666667</prod> <act>Management</act><emp>3123</emp><job>88898</job><chap>-</chap><pg> +0</pg><time>0.5</time><prod>Non-Billable</prod> <act>Management</act><emp>3123</emp><job>22304</job><chap>-</chap><pg> +0</pg><time>0.3</time><prod>Insufficient Information</prod> <act>Management</act><emp>3123</emp><job>11121</job><chap>-</chap><pg> +0</pg><time>1.4</time><prod>Non-Billable</prod> <act>Adapt</act><emp>3123</emp><job>78143</job><chap>08-</chap><pg>0</ +pg><time>0.3</time><prod>Insufficient Information</prod> <act>Import</act><emp>3417</emp><job>76584</job><chap>App K</chap><pg> +4</pg><time>1.0</time><prod>11.4285714285714</prod> <act>Break</act><emp>3123</emp><job>22732</job><chap>-</chap><pg>0</pg +><time>0.4</time><prod>50.65687</prod> <act>key</act><emp>3123</emp><job>78143</job><chap>08</chap><pg>0</pg> +<time>3.3</time><prod>45.5544</prod> <act>Supervision</act><emp>3192</emp><job>54281</job><chap>-</chap><pg +>0</pg><time>4.0</time><prod>Non-Billable</prod>
</root> XML my $t= XML::Twig->new (twig_roots => {emp => \&emp, prod => \&prod}); my %emp; my $currEmp; $t->parse ($xml); print "$_ - $emp{$_}\n" for sort keys %emp; sub emp { my ($t, $data) = @_; $currEmp = $data->trimmed_text (); $emp{$currEmp} ||= ''; } sub prod { my ($t, $data) = @_; my $text = $data->trimmed_text (); return if $text !~ /^\d+(\.\d*)?/; $emp{$currEmp} ||= 0; $emp{$currEmp} += $text; }

Prints:

3017 - 3123 - 96.21127 3192 - 3379 - 6.66666666666667 3384 - 114.285714285714 3417 - 11.4285714285714

DWIM is Perl's answer to Gödel

In reply to Re: Help in manipulating values from two arrays by GrandFather
in thread Help in manipulating values from two arrays by rsriram

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2022-12-01 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?