inkubus has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I have an array of some HEX (or whatever) numbers like:
@myhexarray = '4d 2c 07 2c 77 b3 77 b3';
now, I want to substract ANY object of the array with the following one so I get my result and print it, as:
4d - 2c = 21 2c - 07 = 25 07 - 2c = db 2c - 77 = b5 77 - b3 = c4 b3 - 77 = 3c 77 - b3 = c4
so my result is printed as "21 25 db b5 c4 3c c4"
Thanks!

Replies are listed 'Best First'.
Re: Perl hex substraction
by Ratazong (Monsignor) on Feb 15, 2011 at 12:45 UTC

    Nice problem! I will add this to my list of exercises I use when teaching programming...

    I assume your current lesson is loops. Which kind did you already learn about? (It would be too embarrassing if we would advise you some syntax which your instructor didn't already cover, wouldn't it?).

    The arithmetic shouldn't be a problem at all ... remember the hex-function?

    Thinking twice, it would probably best if you just show us the code you have already written - and where you have doubts... that way we could help you best, without wasting your (and our) time ...

    Have fun learning!

    Rata

      It is kind of a fun problem, because on the surface the student may think he/she needs to implement a whole new form of subtraction for hex numbers... rather than doing the easy thing which is to convert a hex string to a value, and then just print out the result as a hex value. I can see students learing something like operator overloading in a C++ class getting really wrapped around something like this, trying to create a whole new form of subtraction... in Perl, not nearly the same temptation and underscores the importance of referencing strings as values when you need values, and strings when you need strings.

      Then again I remember in an earlier EE course, we were supposed to know how to do hexadecimal subtraction, when we were learning different number bases, longhand... which isn't that tough, especially if you can use 2's complement binary lookups.

Re: Perl hex substraction
by ELISHEVA (Prior) on Feb 15, 2011 at 12:59 UTC

    A few tips to get you started:

    • '4d 2c 07 2c 77 b3 77 b3' isn't an array yet. By assigning it to an array variable as you did, you got an array with one element: a very long string. That probably wasn't what you intended. You'll need split to convert that string into an actual array.
    • Perl has two different ways to loop through array elements: for my $element (@somearray) {...} and for (setup counter; test counter; increment counter), as in C. You'll have to decide which is better to use.
    • When you loop through the array elements to print out each line, be careful about array indexes, especially the choice of the last one.
    • Put use strict;use warnings; at the top of your script if you haven't already. You'll have a better shot of knowing if you made a mistake in your choice of array indexes.
    • And don't forget hex
Re: Perl hex substraction
by Corion (Patriarch) on Feb 15, 2011 at 12:35 UTC

    So, what code have you written, and how does it fail to produce the output you want?

      The problem is I don't know how to do it :)
      I have a file with all those hex numbers like I said: 4d 2c 07 2c 77 b3 77 b3 74 81 9d 59 9d 5f 5a 9e 50 94 4e 8a 4e 8a 4e 8c 43 3c 28 1f 1d 5a 3b fe 3a f1 59 c0 9d f3 54 e2 b2 82 da 68 c1 9b ef bf 9d 59 92 a0 2a 6d 29 ed 29 ed 29 ed 29 ed 29 ed 20 e4 13"
      The "decrypted" result of the file is every hex number is the result of substracting from itself the next hex number.

        How would you do it without a computer? What part of the task do you have difficulties with?

        inkubus:

        Based on your nodes -- above and below -- I suspect you really, really need to read On asking for help and How do I post a question effectively?.

        In fact, reading the next few sentences may illuminate your quest:

        You'll get better answers here if you show that you've made some legitimate effort to solve your own problem. IOW, show us some code and explain how it fails and what error messages and warnings you see. And if you know nothing about programing, at least answer questions like Corion's -- write up a (structured) little list of the steps by which you could solve your problem with pencil and paper (for a liberal definition of "pencil and paper").

        The above answer doesn't do that. But the balance of your interactions in this thread might have gone better had you answered something like this:

        1. Subtract the second number from the first; save the difference somewhere
        2. Subtract the third number from the second; save the difference...
        3. and so on... until...
        4. Subtract ZERO from the last number and save the difference (or, phrased another way, 'simply save the last number').

        That's a simple-minded (and only partial) algorithm to address your request; translating that to code in any language is (just) a matter of matering the language.

        And we're here to help when some aspect of the language stumps you... but we're not here to write your code.

Re: Perl hex substraction
by ikegami (Patriarch) on Feb 15, 2011 at 17:53 UTC

    Hex is a representation of a number. In order to do math, one must first convert it to something the computer considers a number. «hex» can do that.

    You want to do 8 bit arithmetic while ignoring overflows. No problem, just filter out all but the lower 8-bits using «&».

    You will need to convert the result back to hex. «sprintf "%02x"» will do that.

Re: Perl hex substraction
by cdarke (Prior) on Feb 15, 2011 at 13:20 UTC
    07 - 2c = db

    No it is not, it is negative.
    In decimal, 7 - 44 = -37
    37 in hex is 0x25. However -37 is represented as 0xffffffffffffffdb

    I get the following results:
    0x4d - 0x2c = 0x21 (decimal: 33) 0x2c - 0x07 = 0x25 (decimal: 37) 0x07 - 0x2c = 0xffffffffffffffdb (decimal: -37) 0x2c - 0x77 = 0xffffffffffffffb5 (decimal: -75) 0x77 - 0xb3 = 0xffffffffffffffc4 (decimal: -60) 0xb3 - 0x77 = 0x3c (decimal: 60) 0x77 - 0xb3 = 0xffffffffffffffc4 (decimal: -60)
      Don't confuse hex notation with byte values in 1's or 2's complement.

      If you write -37, you can write -0x25 as well. Perl understands it perfectly.

        Yes. Either way, it is certainly not db.
      all those f's are the sign extension, which will vary according to word size. if you had big ints, it'd be even bigger. :)
        I know, but this is not my problem.
        Let's assume I have the following array:
        @names = ("Ann","John","Michael","George","Smith");
        What I want to accomplish is to print them in pairs like:
        Ann John John Michael Michael George George Smith
        Every object with the next one to it in the list...simple, but I don't know how to do that.