Re^2: Perl hex substraction
by JavaFan (Canon) on Feb 15, 2011 at 13:51 UTC
|
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. | [reply] |
|
|
Yes. Either way, it is certainly not db.
| [reply] |
Re^2: Perl hex substraction
by raybies (Chaplain) on Feb 15, 2011 at 13:30 UTC
|
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. :) | [reply] |
|
|
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. | [reply] [d/l] [select] |
|
|
Is this a Perl question, or is it that you have never programmed before and start learning to program using Perl, so it's more like a general programming question? From the way you ask, I suppose the latter, but you should make this clear, because you can get better help if we know about your current level of knowledge.
For instance, Perl offers several ways to iterate over an array, so for a Perl-newbie, I would refer him or her to perlsyn, in particular the sections about looping (because people new to Perl usually don't know which part of the huge documentation contains what they are looking for]. However, if I know that you have never programmed before, this information won't help you, because you don't even know what "looping" means, and we would have to teach you basic skills first.
--
Ronald Fischer <ynnor@mm.st>
| [reply] [d/l] |
|
|
|
|
sounds to me like you're hung up on how to keep pairs of values together. When you have a list.
There're a dozen ways to think of this issue.
One might ask why you have to pair them up at all. If you know the index in the array of the value you're interested in... then the next index is the pair. It's in the list already, why take it appart into pairs.
or if you needed to pull it apart into pairs (or groups of any arbitrary size (let's say instead of pairs, you had to group them into groups of five numbers at a time), which in some languages is done with multidimensional arrays, or you could create tuples--your own custom data type, using lists of lists, or be clever and concatentate the pairs together using an identifier (like you did with the space), or painstakingly keep track of two lists where you once had one...
The sky's the limit, each choice will have consequences, but you have to make a choice and try something out.
| [reply] |
|
|
One way is to iterate over all values and always store the current value, so it becomes the previous value in the next iteration. This way you have both values available at the same time.
The 'tricky' thing is to handle the border case correctly, i.e. when you don't have a previous value, because it's the first iteration... (in the following snippet, this is done by testing if the previous value is defined)
#!/usr/bin/perl -w
use strict;
my @names = ("Ann","John","Michael","George","Smith");
my $prev_name;
for my $name (@names) {
print "$prev_name $name\n" if defined $prev_name;
$prev_name = $name;
}
Output:
Ann John
John Michael
Michael George
George Smith
| [reply] [d/l] [select] |
|
|
|
|