We Recommend

Learning Perl Learning Perl
In this smooth, carefully paced course, a leading Perl trainer teaches you to program in the language that threatens to make C, sed, awk, and the Unix shell obsolete for many tasks. This book is the "official" guide for both formal (classroom) and informal learning. It is fully accessible to the novice programmer.


Posted By

noah on 07/03/07


Tagged

data html generator code heredoc utilities 2005 tab-delimited publish


Versions (?)


Wrap tab-delimited strings in HTML


Published in: Perl 


A simple iterator that takes a tab-delimited file as input and prints an HTML document that contains the tabbed data.

  1. #! /usr/bin/perl -w
  2. use strict;
  3.  
  4. #Time-stamp: <2005-01-12 11:30:05 NSussman>
  5.  
  6. ############################################################
  7. # #
  8. # #
  9. # #
  10. # NOAH SUSSMAN #
  11. # #
  12. # Tabs To Targets #
  13. # #
  14. # Created Jan 12 2005 at 1:10am #
  15. # #
  16. # Given tab-delimited data, place each line into a #
  17. # heredoc and print to STDOUT then go on to the #
  18. # next line #
  19. # #
  20. # Ignore #commented lines and blank lines. #
  21. # #
  22. # #
  23. ############################################################
  24.  
  25. my $input_file= shift; #file containing tab-delimited data
  26.  
  27. open(TABBED_DATA_HANDLE, $input_file) || die("Couldn't open $input_file\n");
  28.  
  29. chomp(my @whole_file=<TABBED_DATA_HANDLE>);
  30.  
  31. foreach my $one_line (@whole_file) {
  32. next if ($one_line =~ m/^\s*$/); #ignore blank lines
  33. next if ($one_line =~ m/^#/); #ignore lines starting with "#"
  34.  
  35. my @record = split(/\t/, $one_line);
  36.  
  37. print "\n";
  38.  
  39. #Place targets in the heredoc.
  40. print <<CODEFRAGMENT;
  41. <!--////////////////$record[2] block of html////////////////////////////////////////////////////////////////-->
  42.  
  43. <tr>
  44. <td class="smelly">$record[1]</td>
  45. <td class="dogfood">$record[0]</td>
  46. <td class="teaParty">$record[2]</td>
  47. </tr>
  48.  
  49.  
  50. <!--////////////////end of $record[2] block of html////////////////////////////////////////////////////////////////-->
  51. CODEFRAGMENT
  52. #end of heredoc.
  53.  
  54. print "\n";
  55. }
  56.  
  57.  
  58. =item
  59. Here is an example data file:
  60.  
  61. big fuzzy bear
  62. little orange kangaroo
  63. large grumpy koala
  64. small sleepy panda
  65.  
  66. =cut

Report this snippet 

You need to login to post a comment.