?
Solved

Change script to select all not just 1.

Posted on 2008-11-06
7
Medium Priority
?
170 Views
Last Modified: 2013-11-05
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

0
Comment
Question by:spudmcc
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:SmartIntel
ID: 22898601
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
 
LVL 5

Expert Comment

by:SmartIntel
ID: 22898934
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
 

Author Comment

by:spudmcc
ID: 22899779
Thanks for quick response.  Tried it and generated the attached error message.  Any help would be appreciated.  

spudmcc
perl-error.png
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:SmartIntel
ID: 22899849
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
 

Author Comment

by:spudmcc
ID: 22900119
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
 
LVL 5

Accepted Solution

by:
SmartIntel earned 2000 total points
ID: 22900171
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
 

Author Closing Comment

by:spudmcc
ID: 31514067
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question