I’m going crazy with a script. Here’s the code:
$parent = @line_elements[8];
$lower_req = @line_elements[1];
print DEBUG “BEGIN \$lower_req is $lower_req, \$parent is $parent, \$req_num is $req_num.\n”;
if ($req_num eq $parent) {
print DEBUG “\$req_num is $req_num, \$parent is $parent, got a match!\n”;
}
And here’s the output:
BEGIN $lower_req is “2.1.1”, $parent is “1.1”
, $req_num is “1.1”.
BEGIN $lower_req is “2.1.2”, $parent is “1.1”
, $req_num is “1.1”.
BEGIN $lower_req is “2.1.3”, $parent is “1.1”
, $req_num is “1.1”.
BEGIN $lower_req is “2.1.4”, $parent is “1.1”
, $req_num is “1.1”.
BEGIN $lower_req is “2.1.5”, $parent is “1.1”
, $req_num is “1.1”.
Note that in each case, that $req_num is equal to $parent, and the line should be repeated with the statement that a match was found. Can another pair of eyes tell me why it’s not?
This will cure whatever it is that ails you: http://www.python.org/
That is just as useful as telling someone with a PC problem to get a Mac. Which is to say, not at all. I don’t have time to learn a new language just to solve this particular problem.
Well, looking at this I don’t see anything wrong – which may mean that there is nothing wrong with this code.
Which means that the error would be in the code that sets $req_num. Is it maybe treated as a floating point number somewhere? Is there an unspeakable (OK, unprintable) character in there?
try adding some quotes:
if (“$req_num” eq “$parent”)
If they’re strings, instead of numbers, maybe eq isn’t a string comparison.
Well, “eq” is the string comparison in Perl, and they are strings (despite the numerals in them) but I’ll give that a shot.
Nope, quoting the variables within the if statement makes no difference.
sorry – I missed the ‘Perl’ in the title, and was parsing the code with the BASH part of my brain – all the while wondering why it ran at all.
Which means that the error would be in the code that sets $req_num. Is it maybe treated as a floating point number somewhere? Is there an unspeakable (OK, unprintable) character in there?
The “code that sets $req_num is a read from a text file, which is read into an array, and then each line of that array is split. What you see (as far as I know) is what you get.
Try testing David’s idea with a length($req_num) and a length($parent).
E.g., these two strings print the same but won’t eq to 1:
$v1 = “1.1”;
$v2 = “1.1”;
That didn’t display correctly. $v1 was supposed to have a backslash 0 in it.
Replace the ‘@’ with a ‘$’ in the array references on the first two lines.
You’re slicing the array instead of indexing it, and $reg_num is probably scalar, which is not equal to a single-element array containing the same scalar value.
Replace the ‘@’ with a ‘$’ in the array references on the first two lines.
Tried that. No joy. Same result.
Try testing David’s idea with a length($req_num) and a length($parent).
Well, that provides a clue. It explains why they don’t match, but not what’s different about them. Length of $parent is 6 and length of $req_num is 5. Now how to figure out what the difference is…
Is there some way to display the whole string with any invisible characters?
I should note that I’m a little crippled because I’m doing this in Windows. Maybe I should upload the files to a Unix account and work from there…
You can print a string with the funny characters translated to backslash escapes with quotemeta,
e.g., print quotemeta($parent)
An straightforward way to examine string content is something like printf ” %x”, unpack(“C”, $c); and procure $c from $req_num with substr() or something. I guess. It was many years since I touched Perl.
It looks like you need to chomp your line. Parent_few is the last thing on your line right? It’s getting the cr in the split.
Ps nice seeing you at mss last week
I’ll second Glen’s comment about ‘@’ and ‘$’. It might not fix the problem, but $var[] is the proper form.
How a string “1.1” gets a length of 5 or 6 totally baffles me. you might try a s/[^\d\.\+-]/x/g; converting characters outside the set of of numeric characters to ‘x’.
What perl package are you using? I usually use the cygwin package if stuck on windoze.
Does each line of output take two lines when you run the script, or is the break caused by the blogging software? If the former, your extra character is a newline. If that is so, then adding
chomp $parent;
as the second line should solve your problem. Note that clearing the newline at the end of the string is probably better handled where @line_elements is being populated.
OK, here is the output of a line generated by the “quotemeta” function:
BEGIN\ \$lower_req\ is\ \”2\.1\.1\”\,\ \$parent\ is\ \”1\.1\”\
\ with\ length\ of\ 6\,\ \$req_num\ is\ \”1\.1\”\ with\ length\ of\ 5\
I’ve looked at it until I’m crosseyed, but I’m still not seeing why one is length of 5 and the other length of 6. I see, though, that there are two backslashes before the first “with” and only one before the second. So what are we not seeing here?
The problem is that your variable $parent is a casualty of media bias. You should make it fair and balanced by removing the carriage return at the end of the string. Here is an example to illustrate the point:
$the_truth = “Palin is the big mav!”;
$msm = $the_truth + “\n”;
if ($msm eq $the_truth)
{ print “You’ll never see it.”; }
By George, I think the people who have called it a newline have called it. I haven’t tried yet, but I’ll bet that’s it. About to go chomp it.
OK, that was it. I wasn’t chomping the line.
D’oh!!
Thanks.
Jim Harris posting for the first time in a while, in order to be an asshole troll, noted. Apparently he (assuming that it actually is a he) is shameless.
Rand, given the timestamps, it looks to me like Jim independently figured out the carriage return issue, and conveyed that info to you in an amusing way. Maybe you don’t like politically-tinged kidding around mixed in with your debugging, but it looks to me like he was trying to help you. I’m only commenting to promote harmony – sorry if I’m offbase.
“dan” wrote: Ps nice seeing you at mss last week
Thanks, Dan, particularly considering that this is in furtherance of an MSS issue…
Yes, Bob, you are off base, because others somehow managed to help me without struggling to pathetically attempt to be “amusing,” or being an asshole.
Do I need to be more explicit about my opinion of “Jim Harris”…?
And I’ll note that attempting to “promote harmony” with him is just about as futile as “promoting harmony” with Hamas or Ahmadinejad. He’s a troll. “Harmony” is not in his vocabulary…
Windows is actually a nice environment in which to do Perl development, if you download the Komodo editor from ActiveState. It’s free for non-commercial use. I have done very extensive Perl development with it for years. It’s sweet, especially the regular expression mini-window that lets you test regex interactively.