hex print

R

Rainer Weikusat

Trifle Menot said:
To print a string


in hex, separated by single spaces


can it be done without looping?

[rw@sable]~#perl -e 'print(join(" ", unpack("(H2)*", "abc123")), "\n");'
61 62 63 31 32 33
 
K

Kaz Kylheku

To print a string


in hex, separated by single spaces


can it be done without looping?

... and for bonus points, while chained and straitjacketed, suspended in a
plexiglass tank full of water, and without using the character $ anywhere?
 
T

Trifle Menot

[rw@sable]~#perl -e 'print(join(" ", unpack("(H2)*", "abc123")), "\n");'
61 62 63 31 32 33

Thanks.

But to minimize it, I think I will eliminate the print() and unpack()
parentheses.

fw:~/temp # perl -e 'print join (" ", unpack "(H2)*", "abc123"), "\n";'
61 62 63 31 32 33

The (H2) parentheses create list context, I suppose? That was the
tricky part I couldn't work out on my own.
 
T

Trifle Menot

.. and for bonus points, while chained and straitjacketed, suspended in a
plexiglass tank full of water, and without using the character $ anywhere?

I do have a need for it, and wanted to find the best way.
 
G

George Mpouras

Στις 25/2/2014 21:48, ο/η Trifle Menot έγÏαψε:
To print a string


in hex, separated by single spaces


can it be done without looping?

$_ = 'abc123';
s/\w/unpack('H2',$&).' '/ge;
print;
 
R

Rainer Weikusat

Trifle Menot said:
[rw@sable]~#perl -e 'print(join(" ", unpack("(H2)*", "abc123")), "\n");'
61 62 63 31 32 33

Thanks.

But to minimize it, I think I will eliminate the print() and unpack()
parentheses.

fw:~/temp # perl -e 'print join (" ", unpack "(H2)*", "abc123"), "\n";'
61 62 63 31 32 33

The (H2) parentheses create list context, I suppose?

It make no sense to talk about 'list context' here as 'pack' and
'unpack' specifiers are written in a formal language other than
perl. The () mark a group here so (for unpack)

(H2)*

means 'as many two-digit hex numbers as can be pulled out of the
input'.
 
T

Trifle Menot

It make no sense to talk about 'list context' here as 'pack' and
'unpack' specifiers are written in a formal language other than
perl. The () mark a group here so (for unpack)

means 'as many two-digit hex numbers as can be pulled out of the
input'.

I thought an asterisk, by itself, meant "as many as can be pulled from
the input." That was one of the things I tried earlier, and failed. But
I threw that code away, so I don't remember exactly what I tried.

The parentheses seem to add some magic. I thought the "magic" was list
context. But I'll take your word for it.
 
C

Charlton Wilbur

T> To print a string
T> in hex, separated by single spaces

T> can it be done without looping?

No. Something will need to iterate; the best you can accomplish is
making it implicit rather than explicit.

Charlton
 
J

John Bokma

Trifle Menot said:
On Tue, 25 Feb 2014 20:50:59 +0000, Rainer Weikusat

[..]

The parentheses seem to add some magic. I thought the "magic" was list
context. But I'll take your word for it.

Don't: perldoc -f pack

"
· A ()-group is a sub-TEMPLATE enclosed in parentheses.
A group may take a repeat count, both as postfix,
"
 
T

Trifle Menot

T> To print a string

T> in hex, separated by single spaces


T> can it be done without looping?

No. Something will need to iterate; the best you can accomplish is
making it implicit rather than explicit.

I know what assembly language is, so I understand what you mean. But
those words may confuse people who only want to learn enough Perl to
solve their immediate problem.

It's not always wise to say everything you know.
 
R

Rainer Weikusat

John Bokma said:
Trifle Menot said:
On Tue, 25 Feb 2014 20:50:59 +0000, Rainer Weikusat

[..]

The parentheses seem to add some magic. I thought the "magic" was list
context. But I'll take your word for it.

Don't: perldoc -f pack

"
· A ()-group is a sub-TEMPLATE enclosed in parentheses.
A group may take a repeat count, both as postfix,
"

Is this supposed to contradict


,----
|The () mark a group here so (for unpack)
|
|(H2)*
|
|means 'as many two-digit hex numbers as can be pulled out of the
|input'.
`----#

?

If so, how?
 
J

John Bokma

Rainer Weikusat said:
John Bokma said:
Trifle Menot said:
On Tue, 25 Feb 2014 20:50:59 +0000, Rainer Weikusat

[..]

The parentheses seem to add some magic. I thought the "magic" was list
context. But I'll take your word for it.

Don't: perldoc -f pack

"
· A ()-group is a sub-TEMPLATE enclosed in parentheses.
A group may take a repeat count, both as postfix,
"

Is this supposed to contradict


,----
|The () mark a group here so (for unpack)
|
|(H2)*
|
|means 'as many two-digit hex numbers as can be pulled out of the
|input'.
`----#

?

If so, how?

No, it's not a contradiction. But a recommendation not to take someone's
word on Usenet for something if one can read the documentation.

Don't take it personal, it was not meant that way, and my apologies if
you read it as such.
 
T

Trifle Menot

[..]

The parentheses seem to add some magic. I thought the "magic" was list
context. But I'll take your word for it.

Don't: perldoc -f pack

"
· A ()-group is a sub-TEMPLATE enclosed in parentheses.
A group may take a repeat count, both as postfix,
"

I read that earlier, when I was trying and failing.

But with so much scalar and list context swirling around in my head, it
just didn't sink in the first time.

Seems clear now though.
 
T

Trifle Menot

No, it's not a contradiction. But a recommendation not to take someone's
word on Usenet for something if one can read the documentation.

Right. I did not take it as a contradiction.

And I agree it's best to read first and ask questions later. But some of
us are slow, and read something 10 times before we get it.

Slow doesn't mean stupid, it just means slow. People learn in different
ways and at different rates. A little help from another human can speed
things along.
 
D

Dr.Ruud

On Tue, 25 Feb 2014 16:29:00 -0500, Charlton Wilbur


I know what assembly language is, so I understand what you mean. But
those words may confuse people who only want to learn enough Perl to
solve their immediate problem.

It's not always wise to say everything you know.

It can also not be done without electricity.
TIMTOWDI: there is (always) more than one (decent) way to do it.

A better general question is:
What practical{, joker} ways are there to ...


Driest is probably:

perl -wE'
my $data = "abc123";
say join " ", unpack "(H2)*", $data;
'
61 62 63 31 32 33
 
T

Trifle Menot

It can also not be done without electricity.
TIMTOWDI: there is (always) more than one (decent) way to do it.

In this case maybe not.

Driest is probably:

perl -wE'
my $data = "abc123";
say join " ", unpack "(H2)*", $data;
'
61 62 63 31 32 33

I consider print vs. say immaterial. The essence of the solution is:

a) unpack a string to a list of hex values
b) rejoin it with spaces

Both steps in one line of code. I don't see how that could be improved.
 
C

Charlton Wilbur

T> On Tue, 25 Feb 2014 16:29:00 -0500, Charlton Wilbur

T> I know what assembly language is, so I understand what you
T> mean. But those words may confuse people who only want to learn
T> enough Perl to solve their immediate problem.

A little learning is a dangerous thing;
drink deep, or taste not the Pierian spring.

T> It's not always wise to say everything you know.

And it is inordinately foolish to ask idiotic questions and expect to
not have the idiocy pointed out.

Charlton
 
T

Trifle Menot

A little learning is a dangerous thing;
drink deep, or taste not the Pierian spring.

T> It's not always wise to say everything you know.

And it is inordinately foolish to ask idiotic questions and expect to
not have the idiocy pointed out.

Charlton

Thanks for the pseudo intellectual advice, Charlton.
 
P

Peter J. Holzer

T> On Tue, 25 Feb 2014 16:29:00 -0500, Charlton Wilbur


T> I know what assembly language is, so I understand what you
T> mean. But those words may confuse people who only want to learn
T> enough Perl to solve their immediate problem.

A little learning is a dangerous thing;
drink deep, or taste not the Pierian spring.

T> It's not always wise to say everything you know.

And it is inordinately foolish to ask idiotic questions and expect to
not have the idiocy pointed out.

The question wasn't idiotic. It was somewhat academic (Solve problem A
without using obvious feature X), but that's often a good way to learn,
and especially in an interpreted language it is often worthwhile to use
complex builtins instead of explicitely coding a loop (less interpreter
overhead).

It is - well, I'd like to avoid words like "foolish" or "idiotic" here -
not useful to give an answer which is obviously true at some level, but
doesn't match the level of the question. Yes, using perl to run Perl
programs always involves loops. The compiler will loop over the source
text building the byte code[1], the interpreter will loop over the byte
code. Many primitive operations will also involve loops. @a = @b will
loop over both arrays. m/foo.*bar/ will loop over $_ and the pattern.
But at the level of the Perl language these are primitive operations,
not loops.

hp

[1] Is there a more generic term for this? It's not *byte* code after
all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top