spudmcc
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
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;
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;
#!/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;
ASKER
Thanks for quick response. Tried it and generated the attached error message. Any help would be appreciated.
spudmcc
perl-error.png
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?
my @codes=('MD','NJ',.....); # Put all the codes here
like
my @codes=('MD','NJ','PA','CA
To my understanding, codes is States short form. Is it correct?
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
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;
Perl.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
spudmcc (Andy)
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.