Home > Archive > PERL Beginners > September 2007 > Editing a text file with perl.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Editing a text file with perl.
|
|
| SerriaRomeo 2007-09-22, 7:00 pm |
| Ok, this is a very newbie problem, I'm just not smart enough to
figure out how to do it.
I have text in a file that looks like this.
0 !~
Syntax: !
! repeats the last command you typed.
~
0 "REMOVE INVIS"~
Syntax: cast 'remove invis' <object>
This spell will make an invisible object in the character's inventory
visible.
This is an AGGRESSIVE spell when used on others. I.e, it will attack
another player if you attempt to use it on them.
~
and I want to reformat each section to something like this:
0 !~
@ @
@ @
|\|========================|\|
|\| |
\|
|\| Syntax: ! |\|
|\| |
\|
|\| ! repeats the last command you typed. |\|
|\| |
\|
|\|========================|\|
@ @
@ @
~
each section is blocked off by #command~ and ~ (Blank Line) at the
end.
Is this possible, if so how would I go about doing it?
| |
| John W. Krahn 2007-09-22, 10:03 pm |
| SerriaRomeo wrote:
> Ok, this is a very newbie problem, I'm just not smart enough to
> figure out how to do it.
>
> I have text in a file that looks like this.
>
> 0 !~
> Syntax: !
>
> ! repeats the last command you typed.
> ~
>
> 0 "REMOVE INVIS"~
> Syntax: cast 'remove invis' <object>
>
> This spell will make an invisible object in the character's inventory
> visible.
> This is an AGGRESSIVE spell when used on others. I.e, it will attack
> another player if you attempt to use it on them.
> ~
>
> and I want to reformat each section to something like this:
>
> 0 !~
> @ @
> @ @
> |\|========================|\|
> |\| |
> \|
> |\| Syntax: ! |\|
> |\| |
> \|
> |\| ! repeats the last command you typed. |\|
> |\| |
> \|
> |\|========================|\|
> @ @
> @ @
> ~
>
> each section is blocked off by #command~ and ~ (Blank Line) at the
> end.
>
> Is this possible, if so how would I go about doing it?
This appears to work:
#!/usr/bin/perl
use warnings;
use strict;
my $head = ( ( '@' . ( ' ' x 66 ) . "\@\n" ) x 2 ) . '|\|' . ( '=' x 24 ) .
"|\\|\n";
my $tail = '|\|' . ( '=' x 24 ) . "|\\|\n" . ( ( '@' . ( ' ' x 66 ) . "\@\n" )
x 2 );
my $format = "|\\| %-64s |\\|\n";
while ( <> ) {
chomp;
if ( /^0.+~$/ ) {
print "$_\n$head";
}
elsif ( /^Syntax:/ ) {
printf $format . $format . $format, ' ', $_, ' ';
}
elsif ( /^~$/ ) {
printf $format, ' ';
print "$tail$_\n\n";
}
elsif ( /\S/ ) {
printf $format, $_;
}
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
|
|
|