UPDATE:

I've implemented most of the suggestions I received and the little program now looks like this:

#!/usr/bin/perl -w #Purpose: To Take a DOS file wildcard and thus take all the matching c +ustom SGML files in the working directory and subdivide them into new + files whose names are the id's of the divs in those original files. use strict; print "What file(s) do you want to run this program on?\n"; our $lines = ""; our @InFileNames; our @OutFileNames; our @OutFileContent; my $i = 0; my $j = 0; my $TheFile = <STDIN>; chomp ($TheFile); #open file and get all text sub OpenFile { open(FILE, $_[0]) or $lines = ""; local $/ = undef; $lines = <FILE>; #remove blank lines $lines =~ s/\n{2}/\n/gms; close(FILE); } #add ¥ to closing div tags sub MarkClose { $lines =~ s/(<\/div>)/$1¥/gms; } #open output.txt for appending and write results to it sub FileAppend { my $Outfile = ">>" . $_[0] . ".bsd"; my $Content = $_[1]; open(FILE, $Outfile) or die "Can't open $Outfile.\n"; print FILE $Content; print FILE "\n"; close FILE; } #Create an array containing all file in the directory matching the glo +b. sub GetInFileList { my $FileDef = $_[0]; @InFileNames = glob($FileDef); } #Populate an array with the contents of the id attribute of every <div +> tag in the input file. sub GetOutFilesList { my $OutFile; @OutFileNames = $lines =~ m/<div[^>]*>/gms; foreach $OutFile (@OutFileNames){ $OutFile =~ s/<div type=[^\s]* id="([^>]*)">/$1/gms; $OutFile =~ s/\./_/gms; } } #Subdivides the File into the subfiles. sub GetOutFileContent { my $LinesString = $_[0]; @OutFileContent = split /¥/, $LinesString; } ### Does the job &GetInFileList($TheFile); for ($i = 0, $i < @InFileNames, $i++) { &OpenFile($InFileNames[$i]); &MarkClose(); &GetOutFilesList; &GetOutFileContent($lines); for ($j = 0, $j < @OutFileNames, $j++){ &FileAppend($OutFileNames[$j], $OutFileContent[$j]); } } #be nice and say it's done print "Program Finished\n";

The script now dies at a very specific point: On line 13 or possibly 18, where it gives the following error message:

Use of uninitialized value in open at ##program name censored## line 18, <STDIN> line 1.

I take this to mean that the array entitled @InFileNames has no contents, because the glob function used to fill it on line 45 didn't behave as I thought it would.

Also, the debug mode behaved oddly when it reached the <STDIN> line, it prompted me for input with the line DB(1), and then prompted me again with DB(2) when I had given it its input, and so on ad infinitum.


In reply to Re: Need help with subdividing SGML files by DukeLeto
in thread Need help with subdividing SGML files by DukeLeto

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.