vnpenguin has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
I would like to write a simple script to do a simple(?) template. I make a template file like:
Section %N1% Sub-section %N2%
By giving N1=1,2 and N2=1,3 I got:
This is my code:Section 1 Sub-section 1 Sub-section 2 Sub-section 3 Section 2 Sub-section 1 Sub-section 2 Sub-section 3
And I run:#!/usr/bin/perl my ($file,$n1,$n2,$tpl); $file = shift; $n1 = shift; $n2 = shift; open(F,'<',$file) || die; while(<F>){ $tpl .= $_; } close(F); if ($n2){ for($i1=1;$i1<=$n1;$i1++){ for($i2=1;$i2<=$n2;$i2++){ $str = $tpl; $str =~ s/%N1%/$i1/gi; $str =~ s/%N2%/$i2/gi; print $str; } } } elsif($n1){ for($i1=1;$i1<=$n1;$i1++){ $str = $tpl; $str =~ s/%N1%/$i1/gi; print $str; } }
./script.pl templatefile 2 3
I'm not Perl coder, just a Perl's fan "amateur". I think there is undoubtedly better way to do this. So I would like to learn from you :)
Thanks,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: simple template ?
by merlyn (Sage) on Sep 10, 2006 at 13:01 UTC | |
by vnpenguin (Beadle) on Sep 10, 2006 at 19:19 UTC | |
|
Re: simple template ?
by madbombX (Hermit) on Sep 10, 2006 at 16:16 UTC | |
by vnpenguin (Beadle) on Sep 10, 2006 at 19:22 UTC | |
by chromatic (Archbishop) on Sep 10, 2006 at 19:52 UTC | |
|
Re: simple template ?
by gloryhack (Deacon) on Sep 11, 2006 at 00:33 UTC | |
|
Re: simple template ?
by mav3067 (Beadle) on Sep 10, 2006 at 20:19 UTC |