Re: the file format does not write to the output file
[email protected] ("John W. Krahn")
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
Aruna Goke wrote:
> If i run the below code it does not right to the voucher.rtf file.
>
> Can you guide on the best way to accomplish this.
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
>
> my ($pin, $pin1, $pin2, $val);
>
> format MINE=
>
>
> Voucher @<<< Voucher @<<< Voucher @<<<
> $val, $val, $val,
>
> PIN @<<<<<<<<<<<<< PIN @<<<<<<<<<<<<< PIN @<<<<<<<<<<<<<
> $pin, $pin1, $pin2,
[snip]
> .
>
>
> my $outvou = '/perl/mycode/progperl/voucher.rtf';
>
> #open the file voucher.rtf for inputs
> open TESTFILE, "+>", $outvou or die "cannot open $outvou:$!";
Is there any reason that you need to *read* from this file as well?
> my $testpins = '/perl/mycode/progperl/testpins.txt';
> #open the file testpins.txt for reading
> open my $fh, '<', $testpins or die "cannot open $testpins:$!";
> my $c;
>
> my @result = <$fh>;
> my @pin;
>
> for(@result){
That should really be:
while ( <$fh> ) {
Because you really don't need to read the whole file into memeory.
> my @line = (split/ /);
You are splitting on a single space character so the first line will
produce the list:
( "5467054961946", "", "200acct\n" )
To fix it change to:
my @line = split;
Which will produce the list:
( "5467054961946", "200acct" )
> push @pin, $line[0] if $line[1] =~/200acct/
Should probably be:
push @pin, $line[0] if $line[1] eq '200acct';
> }
> for($c=0; $c <= $#pin; $c++){
> $pin = $pin[$c];
> $pin1 = $pin[$c=$c+1];
> $pin2 = $pin[$c=$c+1];
$c=$c+1 is usually written as $c += 1. Or you could use auto-increment:
$pin = $pin[ $c ];
$pin1 = $pin[ ++$c ];
$pin2 = $pin[ ++$c ];
You have another problem here in that the number of elements of @pin is
not evenly divisible by three.
> $val = 200;
> select(TESTFILE);
> $~ = "MINE";
200 and TESTFILE and "MINE" never change so there is no good reason to
have these assignments inside the loop.
> write;
> }
>
> close $fh;
> close (TESTFILE);
>
> extract from testpins.txt
> ==========================
> 5467054961946 200acct
> 5518935606936 200acct
> 5559810038657 100acct
> 5840143221892 200acct
> 5604116723026 200acct
> 5490024283962 200acct
> 5657499629585 200acct
> 5899894585741 200acct
> 5750426282431 200acct
> 5939183545596 200acct
> 5621825327200 200acct
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