Parse instruction file

2002-12-18 Thread William.Ampeh
Hello,

I am trying to write a WEB page equation finder for files with the
following contents, and I think I am getting stuck (the program runs very
slowly, and at times I am not able to get all the variables.  So I will
very much appreciate any help.  Basically, I am trying to generate a Hash
of Hash from a text file with the following contents:


---BEGIN DATA
LINES---
type CALCULATIONS FOR TRANSACTION F.203 (NET INTERBANK CLAIMS) ARE
COMPLETE

--**
--**
--CHKDEP
--   F.204CHECKABLE DEPOSITS AND CURRENCY
--  WRITTEN BY DXB ON 10/15/90
--  SERIES OF CALCULATIONS FOR TABLE F.204 -- CHECKABLE DEPOSITS AND
CURRENCY
--**
--- BORROWING
--
-- --- SCBTOTAL ---
set fa713120005.q = fa713123105.q +fa713122605.q +fa713125005.q
   --] MONETARY AUTHORITY = US GOVT
CASH 
   --] DEPS, FOREIGN DEPOSITS, AND
CURRENCY
   --] OUTSIDE BANKS (FROM DEP)
-- --- SCBTOTAL ---
set fa903023105.q =fa723123105.q +fa713123105.q -fa313020005.q --] USG MAIL
FLOAT = US GOVT DEPS AT BANKS
   --] (FROM DEP) AND DEPS WITH THE
MONETARY
   --] AUTHORITY LESS TOTAL CASH
BALANCES
   --] (FROM FISCAL ANALYSIS)
set fa883011105.q = fa313011105.q +fa713011203.q --] US GOLD STOCK AND
SDRS = US GOVT, EX STAB
 --] FUND AND MONETARY
AUTHORITY
 LENDING
---
set fa153020005.q =fa793120005.q -fa313020005.q -fa263020005.q
-fa10302.q 
-fa113020003.q -fa133020003.q -fa213020005.q -fa443020005.q 
-fa47302.q -fa513020003.q -fa543020003.q -fa573020005.q 
-fa223020003.q -fa613020003.q -fa633020003.q  
-fa663020003.q -fa403020003.q -fa743020003.q -fa903029005.q 
-fa603020003.q -fa643020003.q
   --] HOUSEHOLDS (RESIDUAL) = TOTAL
LESS US
   --] GOVT, FOREIGN, NONFIN.
   --] CORP., NONCORP. NONFIN., FARM,
STATE 
   --] LOCAL GOVT, CREDIT UNIONS, PROP

   --] CASULTY INSURANCE, LIFE
INSURANCE,
   --] PRIVATE PENSION FUNDS, STATE 
LOCAL
   --] GOV'T RETIREMENTS, FIN. COS.,
MONEY
   --] MARKET MUTUAL FUNDS, MUTUAL
FUNDS,
   --] BROKERS AND DEALERS, SPONSORED
CREDIT
   --] AGENCIES, AND COMMERCIAL
BANKS,BPT

-END DATA
LINES


So that, with  the following lines of codes:

   foreach $family ( keys %DATA ){
  if ( $family =~ /\d/ ){
  print $family = {,  @{ $DATA{$family} }, }\n;
  }
   }


The script will produce:

$DATA{fa713120005.q} =  {fa713123105.q,fa713122605.q,fa713125005.q}
$DATA{fa903023105.q} = {fa723123105.q,fa713123105.q, -fa313020005.q}
$DATA{fa883011105.q} = {fa313011105.q +fa713011203.q}
$DATA{fa153020005.q} = {fa793120005.q, -fa313020005.q, -fa263020005.q,
-fa10302.q, -fa47302.q, -fa513020003.q, -fa543020003.q,
-fa573020005.q,-fa223020003.q, -fa613020003.q, -fa633020003.q,
-fa663020003.q, -fa403020003.q, -fa743020003.q, -fa903029005.q,
fa603020003.q, -fa643020003.q}



Thank you all.


William


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Parse instruction file

2002-12-18 Thread William.Ampeh
Hello,

This is kind of sloppy, but it is what I have been able to come up with.
How can I make this more cleaner?

The data lines and output are at the end of this mail.


Thank you all.






#!/opt/local/bin/perl
#..End include subroutines 

check_arguments($#ARGV);  #verify command line
process_arguments(@ARGV); #input file = $opt_i
   #output file = $opt_o
open_file(INFILE, , $opt_i);#update mode

my(%MAIN_ADD_DECK)= ();

#  ~~~ READING OF INPUT FILE BEGINS HERE ~~
while ($line = read_from_file(INFILE)) {
   next if ( (!($line =~ /f.\d{9}\../i )) || $line =~ /^\s*--/ ); # weedout
comment  non data lines
   chomp $line;  #remove end of line marker
   if ( $line =~ /--/ ){
  split /--/, $line, 2;$line = join '', $_[0];
   }

   if ( $line =~ /\\/ ){
  split /\\/, $line, 2;$line = join '', $_[0];
   }

   $line =~ s/\s+//g;#finally squeeze out spaces
   $line =~ s/new|set//i;

   #... tag family head to members ONLY if you encounter =
   ($family_head, $line)= split /=/, $line, 2 if ( $line =~ /=/); #tag
family head to members
   push @{ $MAIN_ADD_DECK{$family_head} }, $line;
}
close INFILE;

open_file(OUTFILE, , $opt_o);  #update mode

foreach $family (sort (keys %MAIN_ADD_DECK )){
   print OUTFILE $family = {,  @{ $MAIN_ADD_DECK{$family} }, }\n;
}
close OUTFILE;

#:::  SUBROUTINES :::
sub check_arguments{
   local ($num_args) = @_;
   if ( $num_args  3 ){ #i.e., 0 1 2 3
  error_messages;
   }
}

#^^
# ..displays appropriate error messages ...

sub error_messages{
   print(\n\n\n\nE   R   R   O   R\n);
   print(\n\n   USAGE: $0 -i {excel filename} -o {output filename });
   print(\n\n  EG: $0 -i table1a.xls -o table1a.txt\n\n);
   print(\n\n  -  E X E C U T I O NS U P P R E S S E D  !
! ! - \n\n);
   print (\a\n\n\n);
   exit (1);   #++ failure
}

#^^
# .. distinguish b/n input/output files ...

#^^
sub process_arguments  #determine -d -f -q
{

   #require 'getopts.pl' ;  # do is better than require
   do 'getopts.pl' || die \n\n\n\a\tCOULD NOT LOCATE getops.pl\n\n\n ;

   Getopts('i:o:'); #similar to getopts
 #in sh, but better

   if ( !defined($opt_i) || !defined($opt_o) ){
  error_messages;
   }
}

#^^
#  open file as sepcified .

sub open_file
{
   local ($filevar, $filemode, $filename) = @_;
   open ($filevar, $filemode . $filename) ||
  die(\a Can't open $filename);
}


#^^
#. read fron input file ...

sub read_from_file
{
   local ($filevar) = @_;
   $filevar;
}
#  print to output file

sub print_to_file
{
   local ($filevar, $line) = @_;
   print $filevar ($line);
}


#= END OF PROGRAM



--BEGIN DATA
LINES--
type CALCULATIONS FOR TRANSACTION F.203 (NET INTERBANK CLAIMS) ARE
COMPLETE

--**

--**

--CHKDEP
--   F.204CHECKABLE DEPOSITS AND CURRENCY
--  WRITTEN BY DXB ON 10/15/90
--  SERIES OF CALCULATIONS FOR TABLE F.204 -- CHECKABLE DEPOSITS AND
CURRENCY
--**

--- BORROWING
--
-- --- SCBTOTAL ---
set fa713120005.q = fa713123105.q +fa713122605.q +fa713125005.q
   --] MONETARY AUTHORITY = US GOVT
CASH 
   --] DEPS, FOREIGN DEPOSITS, AND
CURRENCY
   --] OUTSIDE BANKS (FROM DEP)
-- --- SCBTOTAL ---
set fa903023105.q =fa723123105.q +fa713123105.q -fa313020005.q --] USG MAIL
FLOAT = US GOVT DEPS AT BANKS
   --] (FROM DEP) AND DEPS WITH THE
MONETARY
   --] AUTHORITY LESS TOTAL CASH
BALANCES
   --] (FROM FISCAL ANALYSIS)
set fa883011105.q = fa313011105.q +fa713011203.q --] US GOLD STOCK AND
SDRS = US GOVT, EX STAB
 --] FUND AND MONETARY
AUTHORITY
 LENDING

Parse instruction file, with my current program

2002-12-18 Thread William.Ampeh

Hello,

This is kind of sloppy, but it is what I have been able to come up with.
How can I make this more cleaner?

The data lines and output are at the end of this mail.


Thank you all.


Program file:
(See attached file: build_inst.pl)


Data file:
(See attached file: mainadddeck.prn)

Output from my program:
(See attached file: out.prn)


QUESTION:
---

In my program, I have lines  such as:

   next if ( (!($line =~ /f.\d{9}\../i )) || $line =~ /^\s*--/ ); # weedout
comment  non data lines
   chomp $line;  #remove end of line marker
   if ( $line =~ /--/ ){
  split /--/, $line, 2;
  $line = join '', $_[0];
   }

   if ( $line =~ /\\/ ){
  split /\\/, $line, 2;
  $line = join '', $_[0];
   }


Can I make these lines more compact and efficient?  If so, I will very much
appreciate any help.

Thank you all who responded to my earlier mail.
__

William Ampeh (x3939)
Federal Reserve Board


build_inst.pl
Description: Binary data


mainadddeck.prn
Description: Binary data


out.prn
Description: Binary data
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Parse instruction file, with my current program

2002-12-18 Thread William.Ampeh


Hello,

This is kind of sloppy, but it is what I have been able to come up with.
How can I make this more cleaner?

The data lines and output are at the end of this mail.


Thank you all.


Program file:
(See attached file: build_inst.pl)


Data file:
(See attached file: mainadddeck.prn)

Output from my program:
(See attached file: out.prn)


QUESTION:
---

In my program, I have lines  such as:

   next if ( (!($line =~ /f.\d{9}\../i )) || $line =~ /^\s*--/ ); # weedout
comment  non data lines
   chomp $line;  #remove end of line marker
   if ( $line =~ /--/ ){
  split /--/, $line, 2;
  $line = join '', $_[0];
   }

   if ( $line =~ /\\/ ){
  split /\\/, $line, 2;
  $line = join '', $_[0];
   }


Can I make these lines more compact and efficient?  If so, I will very much
appreciate any help.

Thank you all who responded to my earlier mail.
__

William Ampeh (x3939)
Federal Reserve Board



build_inst.pl
Description: Binary data


mainadddeck.prn
Description: Binary data


out.prn
Description: Binary data
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: Parse instruction file

2002-12-18 Thread John W. Krahn
William Ampeh wrote:
 
 Hello,

Hello,

 This is kind of sloppy, but it is what I have been able to come up with.
 How can I make this more cleaner?
 
 [BIG snip]


Ok, you wanted cleaner, perhaps this will help:

#!/opt/local/bin/perl
use warnings;
use strict;

use Getopt::Std;

sub error_messages {
print ERROR;




E   R   R   O   R


   USAGE: $0 -i {excel filename} -o {output filename }

  EG: $0 -i table1a.xls -o table1a.txt



  -  E X E C U T I O NS U P P R E S S E D  ! ! ! -

\a


ERROR
   exit 1;   #++ failure
}

error_messages if @ARGV  4;

my %opts;
getopts( 'i:o:', \%opts );
error_messages unless $opts{'i'} and $opts{'o'};

my %MAIN_ADD_DECK;
my $family_head;
open my $infile, '', $opts{'i'} or die \aCannot open '$opts{'i'}' $!;

#  ~~~ READING OF INPUT FILE BEGINS HERE ~~
while ( $infile ) {
   $family_head = $1 if s/(f.\d{9}\..)\s*=//i;
   push @{ $MAIN_ADD_DECK{$family_head} }, /(f.\d{9}\..)/ig
  if $family_head;
}
close $infile;

open my $outfile, '', $opts{'o'} or die \aCannot open '$opts{'o'}' $!;
for my $family ( sort keys %MAIN_ADD_DECK ) {
   print $outfile $family = {@{ $MAIN_ADD_DECK{$family} }}\n;
}
close $outfile;

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]