Change script to select all not just 1.

Hi All!

I am not a programmer at all just someone that inherited a project that I am stressing over!  

I have included a parser that we use to select specific codes from a large flat file.  We would like to modify the parser to give us all of the codes if desired not just one at a time.  I don't care if it is a separate script just for the "all" option.  Also you will note that the script goes out and selects codes that start at position 67.  This is only half right.  We also have codes that start at 73.  Is there a way to change the script to search for both at the same time rather than having to manually change the script and re-running it?

Any help would be so appreciated!  

spudmcc
#!/usr/bin/perl
 
 
if(@ARGV+0 < 2) {
	die "Usage: perl trcnconv.pl [Input File] [Output File]";
}
 
open FILEIN, "< $ARGV[0]" || die "Could not open file: #!";
open FILEOUT, "> $ARGV[1]" || die "Could not open file: #!";
 
while(chomp($line = <FILEIN>)) {
	if(substr($line,67,2) eq "MD")
	{
		print FILEOUT "$line\n";
	}
}
 
sub trim() {
	my($string)=@_;
	$string =~ s/^\s*(.*?)\s*$/$1/;  
	return $string;

Open in new window

spudmccAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SmartIntelCommented:
Try this change
while(chomp($line = <FILEIN>)) {
        if(substr($line,67,2) eq "MD" || substr($line,73,2) eq "MD")
        {
                print FILEOUT "$line\n";
        }
}

This will check both the positions 67 or 73.
0
SmartIntelCommented:
To match all the codes.

#!/usr/bin/perl
 
if(@ARGV+0 < 2) {
      die "Usage: perl trcnconv.pl [Input File] [Output File]";
}
 
open FILEIN, "< $ARGV[0]" || die "Could not open file: #!";
open FILEOUT, "> $ARGV[1]" || die "Could not open file: #!";
 my @codes=('MD','NJ',.....); # Put all the codes here
while(chomp($line = <FILEIN>)) {
$code1=substr($line,67,2);
$code2=substr($line,73,2);
      if(grep( /$code1/,@codes) || grep( /$code2/,@codes) )
      {
            print FILEOUT "$line\n";
      }
}
 
sub trim() {
      my($string)=@_;
      $string =~ s/^\s*(.*?)\s*$/$1/;  
      return $string;
0
spudmccAuthor Commented:
Thanks for quick response.  Tried it and generated the attached error message.  Any help would be appreciated.  

spudmcc
perl-error.png
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

SmartIntelCommented:
You have to put all the codes which you want to match.
my @codes=('MD','NJ',.....); # Put all the codes here

like
my @codes=('MD','NJ','PA','CA'); # Put all the codes here.

To my understanding, codes is States short form. Is it correct?
0
spudmccAuthor Commented:
Yes, I figured that part out!  Stupid me!  I added the codes but now I generate this error message.  I attached the file and the new script.

Thanks for your help!

spudmcc
#!/usr/bin/perl
  
if(@ARGV+0 < 2) {
      die "Usage: perl trcnconv.pl [Input File] [Output File]";
}
 
open FILEIN, "< $ARGV[0]" || die "Could not open file: #!";
open FILEOUT, "> $ARGV[1]" || die "Could not open file: #!";
 my @codes=('MD','AA','BA','BC','BI','BO','BR','BV','CD','CE','CI','CM','CN','CP','CR','CS','CV','CX','DP','FA','IA','IC','IP','M','MC','ME','MF','MP','MR','MS','NC','ND','NI','NO','NR','NS','NV','OT','PP','QP','QU','RF','RM','SB','SC','SM','SP','ST','TF','TX','WC','WI','WO','WP','WR','ZZ')
while(chomp($line = <FILEIN>)) {
$code1=substr($line,67,2);
$code2=substr($line,73,2);
      if(grep( /$code1/,@codes) || grep( /$code2/,@codes) )
      {
            print FILEOUT "$line\n";
      }
}
 
sub trim() {
      my($string)=@_;
      $string =~ s/^\s*(.*?)\s*$/$1/;  
      return $string;

Open in new window

Perl.png
0
SmartIntelCommented:
just add ; at the end of @codes

my @codes=('MD','AA','BA','BC','BI','BO','BR','BV','CD','CE','CI','CM','CN','CP','CR','CS','CV','CX','DP','FA','IA','IC','IP','M','MC','ME','MF','MP','MR','MS','NC','ND','NI','NO','NR','NS','NV','OT','PP','QP','QU','RF','RM','SB','SC','SM','SP','ST','TF','TX','WC','WI','WO','WP','WR','ZZ');
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spudmccAuthor Commented:
Thank you so much for your time and talent!  I can't tell you how much I appreciate your help in getting this project completed!

spudmcc (Andy)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.