http://www.perlmonks.org?node_id=1012159

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

open(FH,"+>toc1.ncx"); $OH1 = select(FH); open (FILE,'b.txt') or die "$!"; while (<FILE>) { if (m/^(ISBN Number|book title|Cover|Half|Title|Copyright|Content|P +reface):(.*)/) { push(@arr,$2); } } #print @arr; my $i=0; print '<?xml version="1.0" encoding="UTF-8"?>'."\n"; print '<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http:// +www.daisy.org/z3986/2005/ncx-2005-1.dtd">'."\n"; print '<ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy +.org/z3986/2005/ncx/">'."\n"; print '<head>'."\n"; print '<meta name="dtb:uid" content='."$arr[$i++]".'/>'."\n";#ispn + no print '<meta name="dtb:depth" content=""/>'."\n"; print '<meta name="dtb:totalPageCount" content=""/>'."\n"; print '<meta name="dtb:maxPageNumber" content=""/>'."\n"; print '</head>'."\n"; print '<docTitle>'."$arr[$i++]".'<text></text></docTitle>'."\n"; print '<navMap>'."\n"; print '<navPoint id="NavPoint-" playOrder="1"><navLabel><text>'."$a +rr[$i++]".'</text></navLabel><content src=".html"/></navPoint>'."\n"; print '<navPoint id="NavPoint-1" playOrder="2"><navLabel><text>'."$a +rr[$i++]".'</text></navLabel><content src="Cover.html"/></navPoint>'. +"\n"; print '<navPoint id="NavPoint-2" playOrder="3"><navLabel><text>'."$a +rr[$i++]".'</text></navLabel><content src="Title.html"/></navPoint>'. +"\n"; print '<navPoint id="NavPoint-3" playOrder="4"><navLabel><text>'."$a +rr[$i++]".'</text></navLabel><content src="Fm-part.html"/></navPoint> +'."\n"; print '</navMap>'."\n"; print '</ncx>'."\n"; close FILE; select($OH1); close(FH);

Replies are listed 'Best First'.
Re: create .ncx files
by NetWallah (Canon) on Jan 08, 2013 at 06:07 UTC
    Is there a question here ?

    Please see Writeup Formatting Tips for information on how to post code correctly.

    You may also want to read up on how to use Arrays in perl. Information on this is available by typing "perldoc perlintro".

    I would also recommend adding the following to the top of every program you write:

    use strict; use warnings;
    If you need assistance debugging pyour program, please show the content of the file "b.txt" (at least the first few lines).

    It appears that you want to process a template with some data. An experienced programmer would use something like Template::Toolkit.

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Re: create .ncx files
by LanX (Saint) on Jan 08, 2013 at 06:25 UTC
    ehm ...???

    Parlez-vous Français?

    Cheers Rolf

Re: create .ncx files
by CountZero (Bishop) on Jan 08, 2013 at 17:26 UTC
    This is your actual program:
    open( FH, "+>toc1.ncx" ); $OH1 = select(FH); open( FILE, 'b.txt' ) or die "$!"; while (<FILE>) { if ( m/^(ISBN Number|book title|Cover|Half|Title|Copyright|Content|Preface) +:(.*)/ ) { push( @arr, $2 ); } }
    Your print statement will never execute as it was turned into a comment.

    So what is your question? Assume we know nothing about ".ncx"-files.

    Some very preliminary comments are:

    1. You are not using "use strict; use warnings;
    2. You are using two argument open rather than the three argument version.
    3. You are not using lexical filehandles.
    4. "Kidnapping" the standard out filehandle and pointing it to a file is probably a bad idea. It is better to use an explicit filehandle in your print-statements.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics