Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Data HTML

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
US
Hi gurus,

I am working on formatting data and create an html. The data is in text format as below

03/01/2010 29944
03/02/2010 3228
03/03/2010 496
03/04/2010 0
03/05/2010 985
03/06/2010 0
03/07/2010 0
03/08/2010 0
03/09/2010 0
03/01/2010 30000
03/02/2010 3228
03/03/2010 496
03/04/2010 0
03/05/2010 985
03/06/2010 0
03/07/2010 0
03/08/2010 0
03/09/2010 0
03/01/2010
......
......
......

I need to format as below in html

03/01/2010 03/02/2010 03/03/2010 03/04/2010 03/04/2010 .....
29944 3228 496 0 985
30000 3228 496 0 985
...... ....... ..... ..... ...... ... .
...... ....... ..... ..... ...... ... .

Thank You!
dbadmin
 
Try this:

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@dateorder[/blue][red];[/red]
[black][b]my[/b][/black] [blue]%data[/blue][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<>[red])[/red] [red]{[/red]
        [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
        [black][b]my[/b][/black] [red]([/red][blue]$date[/blue],[blue]$val[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red];[/red]
        [gray][i]# keep track of the order in which dates appeared[/i][/gray]
        [gray][i]# (hash keys are not necessarily sorted)[/i][/gray]
        [olive][b]if[/b][/olive] [red]([/red]![url=http://perldoc.perl.org/functions/defined.html][black][b]defined[/b][/black][/url][red]([/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red])[/red][red])[/red] [red]{[/red] [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@dateorder[/blue],[blue]$date[/blue] [red]}[/red]
        [black][b]push[/b][/black] [blue]@[/blue][red]{[/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red]}[/red],[blue]$val[/blue][red];[/red]
[red]}[/red]

[gray][i]# print headings[/i][/gray]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$d[/blue] [red]([/red][blue]@dateorder[/blue][red])[/red] [red]{[/red] [url=http://perldoc.perl.org/functions/printf.html][black][b]printf[/b][/black][/url] [red]"[/red][purple]%-12s[/purple][red]"[/red],[blue]$d[/blue] [red]}[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]# print data[/i][/gray]
[olive][b]for[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$i[/blue]=[fuchsia]0[/fuchsia][red];[/red] [black][b]defined[/b][/black][red]([/red][blue]$data[/blue][red]{[/red][blue]$dateorder[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red][red][[/red][blue]$i[/blue][red]][/red][red])[/red][red];[/red] [blue]$i[/blue]++[red])[/red] [red]{[/red]
        [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$d[/blue] [red]([/red][blue]@dateorder[/blue][red])[/red] [red]{[/red] [black][b]printf[/b][/black] [red]"[/red][purple]%-12d[/purple][red]"[/red],[blue]$data[/blue][red]{[/red][blue]$d[/blue][red]}[/red][red][[/red][blue]$i[/blue][red]][/red] [red]}[/red]
        [black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

It just prints a plain text table, but you can add the HTML bits to it easily.

Annihilannic.
 
Hi Annihilannic,

That worked like a charm. I am not a programmer in perl, so I couldn't figure out what you exactly did. I couldn't reply you early because, I went on an emergency vacation. Any ways Thanks You! much.

Just a small addition. Is it possible to print a "0" as below, if there is no data in the first file for a specific date for all the sets of data?

03/01/2010 03/02/2010 03/03/2010 03/04/2010 03/04/2010 .....
29944 3228 496 0 0
30000 3228 496 0 985
...... ....... ..... ..... 1200 ... .
...... ....... ..... ..... 0 ... .

Thanks,
dbadmin
 
That's a significantly more complex problem, because it involves sorting dates and backfilling missing data.

Here's a hack to my solution which I'm not particularly proud of, but it seems to work. Note that I'm doing a simple lexicographical sort of the dates, so if they happen to be in different years it won't work:

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@dateorder[/blue][red];[/red]
[black][b]my[/b][/black] [blue]%data[/blue][red];[/red]
[black][b]my[/b][/black] [blue]$maxdepth[/blue]=[fuchsia]0[/fuchsia][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<>[red])[/red] [red]{[/red]
        [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
        [black][b]my[/b][/black] [red]([/red][blue]$date[/blue],[blue]$val[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red];[/red]

        [gray][i]# push zeros if this date is missing prior data[/i][/gray]
        [blue]@[/blue][red]{[/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red]}[/red] = [red]([/red][red])[/red] [olive][b]if[/b][/olive] ![url=http://perldoc.perl.org/functions/defined.html][black][b]defined[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red]}[/red][red];[/red]
        [olive][b]while[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/scalar.html][black][b]scalar[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red]}[/red] < [blue]$maxdepth[/blue]-[fuchsia]1[/fuchsia][red])[/red] [red]{[/red] [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red]}[/red],[fuchsia]0[/fuchsia][red];[/red] [red]}[/red]

        [black][b]push[/b][/black] [blue]@[/blue][red]{[/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red]}[/red],[blue]$val[/blue][red];[/red]

        [gray][i]# keep track of maximum depth encountered so far[/i][/gray]
        [olive][b]if[/b][/olive] [red]([/red][black][b]scalar[/b][/black] [blue]@[/blue][red]{[/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red]}[/red] > [blue]$maxdepth[/blue][red])[/red] [red]{[/red] [blue]$maxdepth[/blue] = [black][b]scalar[/b][/black] [blue]@[/blue][red]{[/red][blue]$data[/blue][red]{[/red][blue]$date[/blue][red]}[/red][red]}[/red][red];[/red] [red]}[/red]
[red]}[/red]

[gray][i]# should be smarter[/i][/gray]
[blue]@dateorder[/blue] = [url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%data[/blue][red];[/red]

[gray][i]# print headings[/i][/gray]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$d[/blue] [red]([/red][blue]@dateorder[/blue][red])[/red] [red]{[/red] [url=http://perldoc.perl.org/functions/printf.html][black][b]printf[/b][/black][/url] [red]"[/red][purple]%-12s[/purple][red]"[/red],[blue]$d[/blue] [red]}[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]# print data[/i][/gray]
[olive][b]for[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$i[/blue]=[fuchsia]0[/fuchsia][red];[/red] [black][b]defined[/b][/black][red]([/red][blue]$data[/blue][red]{[/red][blue]$dateorder[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red][red][[/red][blue]$i[/blue][red]][/red][red])[/red][red];[/red] [blue]$i[/blue]++[red])[/red] [red]{[/red]
        [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$d[/blue] [red]([/red][blue]@dateorder[/blue][red])[/red] [red]{[/red] [black][b]printf[/b][/black] [red]"[/red][purple]%-12d[/purple][red]"[/red],[black][b]defined[/b][/black] [blue]$data[/blue][red]{[/red][blue]$d[/blue][red]}[/red][red][[/red][blue]$i[/blue][red]][/red] ? [blue]$data[/blue][red]{[/red][blue]$d[/blue][red]}[/red][red][[/red][blue]$i[/blue][red]][/red] : [fuchsia]0[/fuchsia] [red]}[/red]
        [black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top