CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
What if I Have 10 Furnaces Instead of Only 9?

 



robhazekamp
Novice

Nov 1, 2016, 10:29 AM

Post #1 of 19 (11258 views)
What if I Have 10 Furnaces Instead of Only 9? Can't Post


Code
my $fnc = shift;                          # furnace number 

if (length($fnc) > 2) { # if more than two digits
$mark = substr $fnc, 1, 1; # split into separator
if ($mark eq "-" or $mark eq " ") { # bail out if not blank or hyphen
$run = substr $fnc, 2; # and furnace number
$fnc = substr $fnc, 0, 1; # and run number
}
else { die "\nInvalid furnace/run number....\n"; } # quit
} #furnace number only
elsif (length($fnc) eq 1) {
$run = shift; #run number only
if (length $run == 0) { die "\nInvalid run number\n"; }
}
else { die "\nInvalid furnace/run number....\n"; } # quit

if ($fnc !~ m/[123456789]/) { die "\nInvalid furnace number....\n"; }
if ($run =~ m/[\D]/) { die "\nInvalid run number....\n"; }

my $runnr = "F$fnc" . sprintf "%06i", $run; #assemble furnace and run number

There it is folks so if my furnace is 10 how do I get that to work?

Thanks,

Rob


(This post was edited by FishMonger on Nov 1, 2016, 11:51 AM)


BillKSmith
Veteran

Nov 1, 2016, 9:08 PM

Post #2 of 19 (11248 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

I do not understand what you expect. Please post several examples of your exact input along with the output that you expect. Be sure to include examples that "work" as well as some that do not.

Can we assume that the code you posted is a complete script and the shift's operate on @ARGV? If so, how is it possible to have the first character a space? Do you quote the arguments? If so, tell us what OS you are using.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Nov 2, 2016, 12:13 AM

Post #3 of 19 (11246 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Please tell us the argument(s) passed to your program (if this is a stand-alone program) or your function is this is a function.

One possibility is that your $fnc variable contains more than two characters including a newline character, for example something like "10\n", leading to an abortion of the execution, so that a simple chomp on $fnc might be sufficient to solve your issue.

Otherwise, please show how you run this code.


robhazekamp
Novice

Nov 2, 2016, 4:30 AM

Post #4 of 19 (11239 views)
Re: [Laurent_R] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Hello,

This is something I am trying to help with. The input to the command prompt is
print 10 1
This call fails
print 9 1
This call succeeds
print is the function 10 is the number of the furnace and 1 is the number of the report that needs to be used.
This is a snippet of the total code. We use this to take the report that is exported to our file share is run against a bunch of perl script to sort it and print it out in a certain format. I did not want to post all of that other code because I did not think it was necessary. Any help would be appreciated.


(This post was edited by robhazekamp on Nov 2, 2016, 4:37 AM)


Laurent_R
Veteran / Moderator

Nov 2, 2016, 5:47 AM

Post #5 of 19 (11234 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Hi,

if your first argument is 10, then

Code
my $fnc = shift;

will populate $fnc with 10.

Then, the length of $fnc is neither larger than 2, nor equal to 1, so that the program will go in the :

Code
else { die "\nInvalid furnace/run number....\n"; }

branch of your code.


Perhaps you need to change:

Code
if (length($fnc) > 2) {

to

Code
if (length($fnc) >= 2) {



FishMonger
Veteran / Moderator

Nov 2, 2016, 7:54 AM

Post #6 of 19 (11230 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Why are you using the length() function to check an integer? That doesn't make any sense.

You really should post a short but complete script that demonstrates the problem.

If you're passing 3 args to your script like that, then you probably should be doing something like this:

Code
#!/user/bin/perl 

use strict;
use warnings;

my ($fnc, $num, $report) = @ARGV;

Then apply whatever checks you need to those vars. I can't tell what you're really trying to accomplish so I can't extend the example until you give a better explanation and sample code.

If your actual script isn't to big, then maybe you should post the whole script.


(This post was edited by FishMonger on Nov 2, 2016, 7:57 AM)


robhazekamp
Novice

Nov 2, 2016, 8:48 AM

Post #7 of 19 (11224 views)
Post deleted by robhazekamp [In reply to]

 


FishMonger
Veteran / Moderator

Nov 2, 2016, 8:57 AM

Post #8 of 19 (11220 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Before I look over you perl code, I'll point out a possible typo in your batch script with this line.

Code
if .%1 == .10 goto fncrpt 10

Did you mean to write that as:

Code
if .%1 == .10 goto fncrpt

or

Code
if .%1 == .10 goto fncrpt10



FishMonger
Veteran / Moderator

Nov 2, 2016, 9:09 AM

Post #9 of 19 (11217 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

c:\test>perl -c Rob.pl

Quote
Missing right curly or square bracket at Rob.pl line 298, at end of line
syntax error at Rob.pl line 298, at EOF
Rob.pl had compilation errors.



(This post was edited by FishMonger on Nov 2, 2016, 9:10 AM)


robhazekamp
Novice

Nov 2, 2016, 10:10 AM

Post #10 of 19 (11209 views)
Re: [FishMonger] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Yeah sorry I accidently erased a curly when I was deleting some of my coworkers email addresses from the script. I fixed it.


robhazekamp
Novice

Nov 2, 2016, 10:13 AM

Post #11 of 19 (11206 views)
Re: [FishMonger] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Fixed and still get:

Invalid furnace/run number.....


FishMonger
Veteran / Moderator

Nov 2, 2016, 10:19 AM

Post #12 of 19 (11200 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

How are you executing your batch script when you receive this error?

Based on your batch script code I expect that you're only passing integers but your perl script indicates that you might be passing strings which include numbers otherwise there would be no reason to look for a hypen or anything else in $func.


(This post was edited by FishMonger on Nov 2, 2016, 10:20 AM)


robhazekamp
Novice

Nov 2, 2016, 10:22 AM

Post #13 of 19 (11196 views)
Re: [FishMonger] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Hello,

As stated before from Windows OS Command Prompt.

The input to the command prompt is

print 10 1

This call fails

print 9 1

This call succeeds

print is the function(batch file being called) 10 is the number of the furnace and 1 is the number of the report that needs to be used.
Any help would be appreciated.


Laurent_R
Veteran / Moderator

Nov 2, 2016, 10:54 AM

Post #14 of 19 (11189 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

I think that my earlier post (#5) gives you the reason why it fails when the first argument has two digits and not when it has only one digit. Please go back and read it.


BillKSmith
Veteran

Nov 2, 2016, 11:32 AM

Post #15 of 19 (11185 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

The code in your original post is much to complicated for the cases you have shown. FishMonger has already pointed out that your arguments are always integers (not strings). Your code allows both inputs to be merged into a single string. Your batch file never requires this feature. (Note that the implementation of this feature fails when $fnc consists of more than one digit.) I recommend that you require two separate inputs. Validate them as integers. Replace all of that code with the following three lines.


Code
my ($fnc, $run) = @ARGV; 
die "Invalid furnace number\n" if $fnc > 10 or $fnc < 1;
die "Invalid run number\n" if $run > 9 or $run < 0;


If this does not work, you have still not explained the problem.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Nov 2, 2016, 11:34 AM

Post #16 of 19 (11183 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post


In Reply To
Hello,

As stated before from Windows OS Command Prompt.

The input to the command prompt is

print 10 1

This call fails

print 9 1

This call succeeds

print is the function(batch file being called) 10 is the number of the furnace and 1 is the number of the report that needs to be used.
Any help would be appreciated.

If that's the case then using the length() function and substr() to check for a hypen or a space does not make any sense and is silly.

$fnc should be checked for its numerical value, not its length.


Laurent_R
Veteran / Moderator

Nov 2, 2016, 12:49 PM

Post #17 of 19 (11176 views)
Re: [FishMonger] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

I think we all agree that this is not the right way to check the parameters (especially the arguments should be checked as numerical values). Yet, I am sorry to repeat, THE bug in the OP code causing the program to die is that the length of $fnc is checked for being either more than 2 or 1, so that if this length is 2 (which is the case when the first argument is 10), the program goes into the die statement of the else clause.


FishMonger
Veteran / Moderator

Nov 2, 2016, 1:09 PM

Post #18 of 19 (11174 views)
Re: [Laurent_R] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Yes, but that only addresses one part of the bug. The other part, which IMO is more important, is the needles (and improper) use of the length() function in the first place.

Bill's correction is the proper fix.


(This post was edited by FishMonger on Nov 2, 2016, 1:10 PM)


BillKSmith
Veteran

Nov 2, 2016, 8:12 PM

Post #19 of 19 (11165 views)
Re: [robhazekamp] What if I Have 10 Furnaces Instead of Only 9? [In reply to] Can't Post

Deleting Post #7 was a very bad idea. Most of the replies in this thread refer to files it contained. They no longer make much sense.

Remember that we are trying to help you. Please be careful not to make changes that affect our work.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives