First off, any time you have something like $gocount, $numcount, $charcount etc it's better to put them in a hash as in $count{go}, $count{num} etc. You can put your various alternatives in a hash as well and can then match on an autogenerated alternation.
#!/usr/bin/perl -w use strict; my %translation = ( char => 'VARCHAR2', varchar => 'VARCHAR2', decimal => 'NUMBER', integer => 'NUMBER', bigint => 'NUMBER', bit => 'NUMBER', tinyint => 'NUMBER', smallint => 'NUMBER', numeric => 'NUMBER', money => 'NUMBER', float => 'FLOAT', real => 'REAL', date => 'DATE', time => 'DATE', smalldatetime => 'DATE', datetime => 'DATE', timestamp => 'TIMESTAMP', longchar => 'CLOB', binary => 'BLOB', varbinary => 'BLOB', longbinary => 'LONG RAW', ); my %count; my $rx = qr/@{[join '|', map quotemeta, keys %translation ]}/; while (<>) { if(s/^\s*go\s*$/\/\n/gi) { $count{GO}++; print "TABLESPACE ARSAMS\n"; } s/\b($rx)\b/$count{$translation{$1}}++; $translation{$1}/egi; $count{total}++; print OUT; } print "$_: $count{$_} changes in this file" for sort keys %count;

This is untested code, but should work modulo typos. See Mark-Jason Dominus' excellent Program Repair Shop and Red Flags article series on Perl.com for introductory pointers on how to write better code.

Note that rather than try and open files itself, this script uses @ARGV for its input (so that Perl does error checking for us) and outputs to STDOUT which can be redirected (error checking by the shell). This is both lazy and flexible. Now you can say script file1 file2 file3 > outfile and get output just as requested.

Makeshifts last the longest.


In reply to Re: Matching Question #2 by Aristotle
in thread Matching Question #2 by curtisb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.