in reply to perl unpack and matching the unpacked data

Corion has most probably given you the solution, but there are various ways your code could be shorter and possibly faster (if that matters). At the very least you could have:
if ($data eq "8000" or $data eq 0080){ $tid = "TEST"; $buf = $test; } elsif ($data eq "8100" or $data eq "0081"){ $tid = "TOOL"; $buf = $tool; } elsif ...
This would make your code twice shorter and probably a bit faster. You could also notice that $tid is "CEX" in most of the cases, so you could reorganize part of your tests:
$tid = "CEX" if grep {$_ eq $data} qw /8300 0083 ... 8F00 008F/;
There are other possible shortcuts, but using too many of them might lead to less clear code. But I think you should think of some basic ones at least. You could also "normalize" $data before testing, i.e. if $data is "00xx", change it to "xx00" and do your tests afterwards, that would also reduce your code by about half.

Replies are listed 'Best First'.
Re^2: perl unpack and matching the unpacked data
by james28909 (Deacon) on Jul 17, 2014 at 21:49 UTC
    isnt something like:
    $data eq "8000" or $data eq "0080" the same as: $data eq "8000" || $data eq "0080";
    i thought i read somewhere that its was better to use "||" instead of "or" and use "&&" instead of "and". anyways...like i said i have tried alot of different avenues, i have even tried only swapping the 8A-8F/A0 with eq to no avail.
    i have been up for a while though so maybe i will take a break from it for a while and get some rest and come back to it with a fresh mind lol. I am looking forward to sharing the tool and the code to see any criticism or input on how to make it better, because honestly this is my first ever program ive done that actually amounted to anything, and its going to have a gui :D
    everything else works great except for this snippet lol. but ill take a break and come back later.
    also thanks for the input :)

      || and or (and, similarly, && and and) have the same semantics in principle, but the former has a higher precedence than the latter. eq has a higher precedence than either, so it doesn't make any difference which one you use there, but it can be important in other contexts.

      Operator Precedence and Associativity has more, as does chapter 3 of Programming Perl.

      isnt something like:
      $data eq "8000" or $data eq "0080" the same as: $data eq "8000" || $data eq "0080";

      A little experimentation should reveal that Perl thinks it is:

      c:\@Work\Perl>perl -wMstrict -MO=Deparse,-p -le "my $data = '8000'; ;; if ($data eq '8000' or $data eq '0080') { print 'yes 1'; }; if ($data eq '8000' || $data eq '0080') { print 'yes 2'; }; " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; (my $data = '8000'); if ((($data eq '8000') or ($data eq '0080'))) { print('yes 1'); } if ((($data eq '8000') or ($data eq '0080'))) { print('yes 2'); } -e syntax OK

      ... i have ... tried ... swapping the 8A-8F/A0 with eq to no avail.

      Again with the experimentation:

      c:\@Work\Perl>perl -wMstrict -le "my $data = '8000'; ;; if ($data eq '8000' or $data eq '0080') { print 'yes 1'; }; if ($data eq '8000' || $data eq '0080') { print 'yes 2'; }; " yes 1 yes 2
      I don't know what code you tried when you used  eq (the proper comparator for strings), but clearly it should work. At the same time, I would also agree with other respondents that a re-organization of the code to avoid an eye- and mind-bezoggling onslaught of if-statements would be very valuable just for the great increase in readability and maintainability I would expect it to yield.

      i thought i read somewhere that its was better to use "||" instead of "or" and use "&&" instead of "and".

      The problem with  and and  or is that these logical operators have such low precedence: their precedence is lower than  , (comma), i.e., lower than whale dung. This was done so that a statement like
          open my $fh, '<', $filename or die "opening '$filename': $!";
      would work as expected. Unfortunately, such low precedence has some surprising side-effects, hence  && and  || are recommended for 'normal' usage, a recommendation I would endorse since I've stumbled over these odd side-effects myself.
      Update: Here's an example of a precedence-induced difference between  ! and  not. This code produces warnings, but you may not be so lucky.

      c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my ($x, $y) = (4, 5); ;; my @ra = (1, 2, ! 3, $x, $y); dd \@ra; ;; my @rb = (1, 2, not 3, $x, $y); dd \@rb; ;; print qq{but \$x and \$y are still $x and $y}; " Useless use of a constant (3) in void context at -e line 1. Useless use of private variable in void context at -e line 1. [1, 2, "", 4, 5] [1, 2, ""] but $x and $y are still 4 and 5

        ok im going to go ahead and give it another shot, and this time i will write it in this format:
        if ( $data eq "8000" || $data eq "0080" ) { $tid = "TEST"; $buf = "TEST SYSTEM"; } elsif ( $data eq "8000" || $data eq "0080" ) { $tid = "TOOL"; $buf = "TOOL SYSTEM"; } elsif.....

        i didnt try it exactly like this. what i tried was actually:
        elsif ( $data eq "8000" || "0080" ) { $tid = .....; }
        i think that will work. i will come back and update the thread when i come to a conclusion.