Perl Problem

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?

28 thoughts on “Perl Problem”

  1. 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.

  2. 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?

  3. try adding some quotes:
    if (“$req_num” eq “$parent”)

    If they’re strings, instead of numbers, maybe eq isn’t a string comparison.

  4. 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.

  5. 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.

  6. 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”;

  7. 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.

  8. 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…

  9. 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.

  10. 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

  11. 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.

  12. 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.

  13. 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?

  14. 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.”; }

  15. 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.

  16. 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…

  17. 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.

Comments are closed.