Re: Missing the if statement and going directly to else
by Coruscate (Sexton) on Jan 17, 2003 at 00:44 UTC
|
@main::fields = split(/\|/, $_);
I cannot imagine why you are doing this. Accessing the variable from the main scope goes against good programming ideologies. The point of being able to pass arguments to a subroutine, whether scalar, hash, array, or reference, is to avoid directly accessing data from that exists outside of the subroutine.
Also, I'm wondering whether or not you looked at the code I provided. From that code, it is quite simple to call a seperate subroutine based on whether or not the part number appears in the flat text file:
#!/usr/bin/perl -w
use strict;
sub get_part_num {
my $part;
while (!defined $part || $part =~ /[^0-9]/) {
print "Part Number? (None to exit) ";
chomp( $part = <STDIN> );
}
die "Program terminated.\n" if $part eq "";
return $part;
}
sub search {
my ($file, $part) = @_;
my $found_match = 0;
open my $db, "<$file" or die "Could not read file: $!";
while (<$db>) {
chomp;
my @fields = split(/\|/, $_);
if ($fields[0] eq $part) {
++$found_match;
found_match(@fields);
last;
}
}
no_match($part) if $found_match == 0;
}
sub found_match {
my ($part_num, @data) = @_;
print "Part $part_num found: \n\t",
join("\n\t", @data), "\n";
}
sub no_match {
my $part_num = shift;
print "Part #$part_num was not found in the database.\n";
}
search( 'fai.txt', get_part_num() );
| [reply] [d/l] [select] |
|
I have looked at your code, some of it I do not understand. I am in the process of learning the basics and I do not want to just copy someone else's code to solve my problem. I want to try and learn it myself. I know that there are easier and better ways to accomplish what I am trying to do. As I grow in the language I will be able to tweek it.
The part numbers are in this format '68-2455-00' in the text file the part number is followed by several other fields all seperated by '|' When the part number is matched I want to print out the entire string that follows the part number in the text file. I did not see how to do this using the code you provided mainly because it uses some stuff that I have not learned about yet. I have only been learning Perl for about 1 1/2 weeks.
That is why I am doing the hack job on it that i am doing. Also why I am spendiing several hours a day here learning.
Kerry
| [reply] |
|
...some of it I do not understand... I do not want to just copy someone else's code to solve my problem. I want to try and learn it myself... it uses some stuff that I have not learned about yet
Kudos to you! You don't know how surprised I was to read this. Many people would have pretended to understand what the code (that I posted) was doing and copied it. I've seen many people (not on this site, but in other places in general) that take the easiest way out.
As for actually solving your problem, I'll just ask one thing? Is it urgent that you get this program working? Do you have a strict deadline that you need to accomplish the task in? If not, I suggest that you take the time (though I see you said "why I am spending several hours a day here learning.", so I suppose that you're already doing so) to learn the language a little more before trying to do anything too complicated. Programming languages are like spoken/written languages: you don't go to France and expect to communicate with the local people after a week and a half of studying French :). The coolest thing with programming languages (the part that separates it from spoken/written languages) is that you can easily experiment with it. You can write a script and test it to see if it does what you were hoping on, but it's harder to call up a French person and trying to speak with them and seeing if they understand you. As well, if this program/script you need is being done for somebody else and you maybe have a deadline for it is to try and get more time to do it in. Your boss (assuming you have one) should be happy to oblige if they expect to get a quality program written. s/he is kidding him/herself if s/he expects someone to learn a whole new programming language in 2 weeks. Though I suppose some bosses are so incredibly impossible and greedy.
One more thing I recommend to you: ask questions. You say you don't understand parts of the script I posted. Don't be afraid to ask me to clarify some portions of it. If you honestly don't understand the way in which any of it is setup, tell me. I can post it again with tons on comments to clarify things.
Good luck to you!
| [reply] |
|
Coruscate++. Wish I had multiple accounts so I could ++ you even more!
-derby
| [reply] |
Re: Missing the if statement and going directly to else
by tadman (Prior) on Jan 16, 2003 at 23:28 UTC
|
| [reply] [d/l] [select] |
|
The part number is declared earlier in the script. It is user input. The part number is in this format 68-2208-04.
It worked before I put the else statement in there. I was trying to get the program to go to a different sub if the user enters a part number that is not in the text file.
| [reply] |
|
If you're trying to print an error if the user enters a part number that is not in the file, and the file contains more than one line, then you are going to have to wait until you've finished reading the file before putting up the error.
I think your if is working correctly, the problem is simply that if you type in a part number that appears on line 2 of the file, you test line 1 first and fail, restarting.
Maybe what you mean is something like this:
sub search
{
my ($PartNumber) = @_;
while (<FILE>)
{
if ($main::fields[0] eq $PartNumber)
{
# ...
return 1; # Ends search() function
}
}
close(FILE);
return; # Returns empty handed
}
This search routine returns 1 if it found a match, undef otherwise. You can put this in a routine which asks for input, like this:
$|++;
my $what;
do
{
print "PartNumber? ";
chomp($what = <STDIN>);
} while (!search($what));
This loop will continue to run until the search() function returns a true value. That will only happen if a match is found.
| [reply] [d/l] [select] |
Re: Missing the if statement and going directly to else
by Cody Pendant (Prior) on Jan 17, 2003 at 02:44 UTC
|
On a purely administrative note, you're not being a pest by asking questions, but you are being kind of a pest by not posting things in the approved way.
It would really help if you put <code> tags around your code -- this would stop "fields[0]" from coming out like "fields[0]" for a start.
--
Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer. M-J D | [reply] |
Re: Missing the if statement and going directly to else
by runrig (Abbot) on Jan 16, 2003 at 23:22 UTC
|
put a print "[$main::fields[0]][$PartNumber]\n"; before the if statement, and let us know what the results are. | [reply] [d/l] |
|
It prints the first record in the text file and then skips to the line that matches and prints that. Then it goes into the sub called in the else part of the block.
| [reply] |
|
Then it does not print the part number? Funny, an empty regex matches any string. Or perhaps the PartNumber has a newline and/or carriage return?
| [reply] |
Re: Missing the if statement and going directly to else
by Abigail-II (Bishop) on Jan 17, 2003 at 01:20 UTC
|
| [reply] [d/l] |
|
The entire is actually in my scratchpad
| [reply] |
|
Your program is doing exactly what you tell it to do. It reads the first line of input from the file, and if it doesn't match, the program exits. That's obviously not what you want it to do, so do something else. Don't exit until either there is a match or you've read the entire file.
Also, you use lexical 'my' variables in one place and package '$main::' package variables in another for no good reason. 'use strict' is a good habit to get into when you're just learning, and if you want globals, put them in a 'use vars' at the top of the script. It'll catch typos in variable names and can save lots of debugging time, and save you from typing 'main::' on all the variables (since 'use vars' declares them in the current package, you don't have to specify the package). If you think this script is too short to bother with 'use strict', then there's no point in the 'my' and 'main::' either.
| [reply] |
|
|
Re: Missing the if statement and going directly to else
by Gilimanjaro (Hermit) on Jan 17, 2003 at 00:33 UTC
|
I'm not sure (haven't tested) but it might be that perl is confused by you using 'continue' as a a function name...
'continue' is also the keyword used to immediately start the next iteration of your while loop...
Try using 'kontinue' as the function name to see what happens... | [reply] |