Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Formatted Input

Moderators: larsivi kris

Posted: 04/05/09 00:37:41

Hi all,

I was wondering what the best way to gather formatted input would be. For instance, take the example below which is an extract from my router's front page. Is there a smart way of extracting information such as uptime in days, hours, etc. and the amount of date received?

Dan

<tr><td> Connection time </td>
<td> 9 days, 1:23:12  </td> </tr>
<tr><td  colspan='2'> <img src='/images/spacer.gif' height='5' width='100%' alt=''></td></tr>
<tr><td class='black'  colspan='2'> <img src='/images/spacer.gif' height='1' width='100%' alt=''></td></tr>
<tr><td  colspan='2'> <img src='/images/spacer.gif' height='20' width='100%' alt=''></td></tr>
<tr><td class='black'  colspan='2'> <img src='/images/spacer.gif' height='1' width='100%' alt=''></td></tr>
<tr><td  bgcolor='#F6F7FC'  colspan='2'> <img src='/images/spacer.gif' height='5' width='100%' alt=''></td></tr>
<tr><td bgcolor='#F6F7FC'> Data transmitted </td>
<td bgcolor='#F6F7FC'> 220.55 MB </td> </tr>
<tr><td  bgcolor='#F6F7FC'  colspan='2'> <img src='/images/spacer.gif' height='5' width='100%' alt=''></td></tr>
<tr><td class='black'  colspan='2'> <img src='/images/spacer.gif' height='1' width='100%' alt=''></td></tr>
<tr><td colspan='2'> <img src='/images/spacer.gif' height='5' width='100%'alt='' ></td></tr>
<tr><td> Data received </td>
<td> 2.82 GB</td> </tr>

Author Message

Posted: 04/05/09 04:22:21

What you have there appears to be well-formed XML. If so, you could probably use tango.text.xml to parse it?

Posted: 04/05/09 08:58:45

Indeed my input is well formatted HTML with the data being held in a very large table (as can partially be observed in the example). I feel that trailing through all the table fields to find the data would not be much use as you then still have to find if it's the right field you're looking at which is probably no better than a search of some type. From a bit of reading I think a Regex may be good but I'm still figuring out how to do it without foreach loops I don't seem to need.

The regex would be something along the lines of "(\d{1,2})\sday[s],\s(\d{1,2}):(\d{1,2}):(\d{1,2})".

Posted: 04/05/09 09:06:35

The XML API (namely the Document) class should make it simple to extract the exact fields you want, maybe TutXmlPath is helpful?

Posted: 04/07/09 20:48:29

Yes, thank you, that is useful. However it still leaves me with a string such as "9 days, 1:23:12". How would you suggest this data is extracted from this string. Would you use C's scanf or does Tango provide something else?

Posted: 04/07/09 21:27:19

No, Tango doesn't have formatted input beyond C scanf bindings. The alternative (which often is fairly easy in D) is to just split (tango.text.Util) and slice what you need (and convert if that is something you need), that is, doing it manually.

Posted: 04/08/09 18:24:00

OK, thanks.