mhoang has asked for the wisdom of the Perl Monks concerning the following question:

I have text file, I like to split in multiple arrays so I can output as muliple tables on same html page Each table will collect the data below the title symbol $...? I am trying to do it with hash of arrays but I am stucked with reference syntax here input file. I couldn't think properly today. Thanks heap!

"$Bulletin Summary?" "$Person Absences?" "No","Person","Code","Period","Absence Reason" "3","B Gra","GAU","All Day","Parent Leave" "5","C Jema","JBD","Day 5 Before School; P (0.2)","Personal Leave" "6","G Eenza","CBA","All Day","Long Service Leave" "7","S Hanie","SBO","4 (1.0)","Meeting" "$Lesson Cancellations?" "Period","Roll Class","Class","Teacher","Room","Reason" "1 (1.0)","12","Common","","HLN","Camp" "2 (2.0)","12","Common","","HLS","Camp" "3 (3.0)","12","AC033B","GANLIS Pet","Y207","Camp" "4 (4.0)","12","EN013B","THUM Jil","Y217","Sick" "$Person Calendar Activities?" "Activity","Start Period","End Period ","Teacher Codes" "LSL","Day 1 Before School (05/29/2017)","GS (08/25/2017)","MHA" "Leave","Day 8 Before School (06/08/2017)","GS","LGR" "LWOP","Day 6 Before School (06/19/2017)","GS","ARO" "South Africa Trip","Day 9 Before School (06/22/2017)","GS","DCA;KGA;B +KI" "$Kids Calendar Activities?" "Activity","Start Period","End Period ","Teacher Codes","Roll Class Co +des","No. Students" "12 Reteat Friday","1","GS","RBR;CBU;TCI;JEM;BHO;RMA;EMG;AMO;KPA;BRY;G +SC;HSI;PSW;KWI","","0" "$Kids Attendance Changes?" "No student attendance changes have been recorded for this day" "$Replacement Persons?" "Period","Room/Area","Class","Roll Class","Absent Teacher","Replacemen +t Teacher","Absent Reason","Load","Count","Notes" "P (0.2)","A107","Pastoral-BRY","12","R Ber","S Peter","Camp","0.2","0 +.0","" "1 (0.2)","A107","Pastoral-PSE","12","S Peter","S John","","0.2","0.0" +,"" "2 (0.2)","Y214","Pastoral-ARO","12","R Anh","Di Cesar Mose","Leave Wi +thout Pay","0.2","0.2","" "3 (0.2)","Y106","Pastoral-ACO","11","C Anche","J Miky","Personal Leav +e","0.2","0.0","" "4 (0.2)","Y106","Pastoral-MJE","11","JENKINSON Mark","JENKINSON Mark" +,"","0.2","0.0","" "$Room Changes?" "Period","Roll Class","Class","Teacher","Room","New Room" "P (0.2)","12","Pastoral-BRY","S John","A106","A107" "2 (0.2)","11","Pastoral-ACO","J Myki","Y107","Y106" "2 (0.2)","10","Pastoral-KPA","De Rest Anni","Y002","Y205" "3 (1.0)","11","RE0111","Woland Can","Y220","LTH" "4 (1.0)","11","RE0112","Woland Can","Y219","LTH" "4 (1.0)","11","RE0113","Da Charlott","Y105","LTH"
Nirvana is Now or Never

Replies are listed 'Best First'.
Re: split the text file to multiple arrays
by huck (Prior) on Jul 05, 2017 at 05:59 UTC

    use strict; use warnings; my @tables; my %data; my $last=''; while (my $line=<DATA>) { chomp $line; if ( substr($line,0,2) eq '"$' || substr($line,0,1) eq '$') { $last=$line; push @tables,$line; } push @{$data{$last}},$line; } use Data::Dumper; print Dumper(\@tables); print Dumper(\%data); __DATA__ "$Bulletin Summary?" "$Person Absences?" "No","Person","Code","Period","Absence Reason" ...

      please help me to check why my code does not display the table in this format. Really appreciated ur zen time :)

      Persons Absences

      table data

      Replacement Persons

      table data

      Room Changes

      table data

      #!/usr/bin/perl use strict; use warnings; use File::Copy; use File::stat; use Time::localtime; #testing my $dest = "C:/Perl-Script/"; my ($date,$time) = &GetTodaysDate; my $space = '1'; open OUT, ">C:/Perl-Script/Bulletin.html"; print OUT "<HTML><head><title>My Learning Page</title></head>"; print OUT "<h2><font color=#ffffff</font>My Home Page</h2>"; print OUT "<h2>Bulletin Date $date $time</h2></style>"; print OUT "<body bgcolor=#002b46>"; my %hdata = &build_hash(); foreach my $key (sort keys %hdata) { my @arrays = @{$hdata{$key}}; if (($key eq '"$Persons Absences?"')|| ($key eq '"$Replacement + Persons?"') || ($key eq '"$Room Changes?"')){ print OUT "\n\n"; print OUT "<h3>$key</h3>\n"; print OUT "<table border = 1 width = 1000 bordercolor=#e0e +0e0 cellpadding = 5 align=left>"; #print " $key = $data{$key}[$i]\n"; #$i= 0 is the title of + table for my $i(1..$#arrays) { my @col = split(/,/,$arrays[$i]); for my $j (1..$#col) { $col[$j] =~ s/"//g; my $td = ($i==1 || $j==1)? "th" : "td"; my $bg = ($i==1)? "bgcolor=#e0e0e0" : "bgcolor=#ff +ffff"; print OUT "<$td $bg align = left>$col[$j]"}; }; }; }; print OUT "</table>"; print OUT "</body>"; print OUT "\n</HTML>\n"; close(OUT); #--------------------------------------------------------------------- +------ # This subroutine split text file to number of arrays # for data processing # -------------------------------------------------------------------- +------ sub build_hash() { my @tables; my %data; my $last =''; open my $ifh,'<',"C:/Perl-Script/Bulletin.txt"; while (my $line=<$ifh>) { chomp $line; if ( substr($line,0,2) eq '"$' || substr($line,0,1) eq '$') { $last=$line; push @tables,$line; } push @{$data{$last}},$line; return (%data); } #--------------------------------------------------------------------- +------ # This subroutine gets the local date and time using Time::Localtime m +odule and # formats it into dd/mm/yyyy and HH:mm. Returns $realTime, $realDate # -------------------------------------------------------------------- +------ sub GetTodaysDate($){ #For Testing my $source = "C:/Perl-Script/NBulletn.txt"; my $target = "C:/Perl-Script/Bulletin.html"; my $text_timestamp = ctime(stat($source)->mtime); my $html_timestamp = ctime(stat($target)->mtime); my ($WDATE, $MONTH, $DAY, $TIME,$YEAR) = split(" ",$html_timestamp); my $datelimtr = " "; my $dtdlimtr = "/"; my $tmdlimtr = ":"; my $realDate=($WDATE . $datelimtr . $DAY .$dtdlimtr . $MONTH . $dtdlim +tr . $YEAR); my $realTime=($TIME); return $realDate, $realTime; }
      Nirvana is Now or Never

        Your code does not compile

        Missing right curly or square bracket at 1194338.pl line 100, at end of line
        syntax error at 1194338.pl line 100, at EOF
        1194338.pl had compilation errors.
        

        Try

        #!/usr/bin/perl use strict; use warnings; use Time::Piece; # current date/time my $t = localtime; my $date = $t->wdayname.' '.$t->dmy('/'); my $time = $t->hms; # input data my $dest = "C:/Perl-Script/"; my %hdata = build_hash($dest."Bulletin.txt"); # output html open OUT, ">". $dest."Bulletin.html" or die "$!"; print OUT qq( <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>My Learning Page</title> <style type="text/css"> body { background-color: #ccccff; } h2 { color: #0; font-size: 2em; } th { background-color: #e0e0e0 } td { background-color: #ffffff } </style> </head> <body> <h2>My Home Page</h2> <h2>Bulletin Date $date $time</h2>\n); foreach my $key (sort keys %hdata) { if (($key eq '"$Persons Absences?"') || ($key eq '"$Replacement Persons?"') || ($key eq '"$Room Changes?"')){ print OUT qq!<h3>$key</h3> <table border="1" width="100%" style="border-color:#e0e0e0" cellpa +dding="5" align="left">\n!; my @arrays = @{$hdata{$key}}; for my $i (1..$#arrays) { print OUT '<tr>'; my @col = split(/,/,$arrays[$i]); for my $j (1..$#col) { $col[$j] =~ s/"//g; my $td = ($i==1)? "th" : "td"; print OUT qq!<$td align="left">$col[$j]</$td>!; }; print OUT "</tr>\n"; }; print OUT "</table>&nbsp;<br>\n"; }; } print OUT "</body></html>"; close OUT; #------------------------------------- # This subroutine split text file to # number of arrays for data processing # ------------------------------------- sub build_hash{ my $infile = shift; my %data; my $last =''; open my $ifh,'<',$infile or die "$!"; while (my $line=<$ifh>) { chomp $line; if ( substr($line,0,2) eq '"$' || substr($line,0,1) eq '$') { $last = $line; } push @{$data{$last}},$line; } close $ifh; return %data; }
        poj