Link to home
Start Free TrialLog in
Avatar of spudmcc
spudmccFlag for United States of America

asked on

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

Avatar of SmartIntel
SmartIntel
Flag of United States of America image

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.
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;
Avatar of spudmcc

ASKER

Thanks for quick response.  Tried it and generated the attached error message.  Any help would be appreciated.  

spudmcc
perl-error.png
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?
Avatar of spudmcc

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of SmartIntel
SmartIntel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of spudmcc

ASKER

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)