Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Matching a hex value in a string...

by s2cuts (Acolyte)
on Feb 27, 2008 at 10:24 UTC ( #670573=perlquestion: print w/replies, xml ) Need Help??
s2cuts has asked for the wisdom of the Perl Monks concerning the following question:

I have been working away at a problem for some time now. I've gotten down to probably the last hoop I need to jump through, and I could really use some help. I'm capturing a stream (scalar) through the Net::Telnet module. This stream contains multiple lines, which are split up by a special character (hex value is 0a, see the output of Dump_Log below). I need help in understanding how to match on it so that I can run the scalar through the "split" function to break it up into an array. That's it. That's what I need to know. I've included my attempt at coding this, which is not working for some strange reason. I'm hoping someone here can see the error I've made. Thanks for any help.

Code snippet using 'hex' to determine the value to match on:
$results = $obj->waitfor($cmd); $break = hex '0a'; @return_lines = split ( $break, $results ); print @return_lines

Just a few lines from Dump_Log, what looks like a period is actually the line break, and it maps to 0a as you can see.
< 0x000d0: 20 75 6e 6c 69 6d 69 74 65 64 20 20 20 30 0a 75 unlimi +ted 0.u < 0x000e0: 73 65 72 31 20 20 66 61 6c 73 65 20 67 65 6e 5f ser1 f +alse gen_ < 0x000f0: 75 73 65 72 20 67 65 6e 5f 75 73 65 72 20 75 73 user ge +n_user us < 0x00100: 65 72 31 40 67 6d 61 69 6c 2e 63 6f 6d 20 20 20 er1@gma < 0x00110: 75 73 65 72 31 20 20 20 20 20 20 75 6e 6c 69 6d user1 + unlim < 0x00120: 69 74 65 64 20 20 20 30 0a 0a 20 47 72 6f 75 70 ited +0.. Group

Replies are listed 'Best First'.
Re: Matching a hex value in a string...
by moritz (Cardinal) on Feb 27, 2008 at 10:40 UTC
    A 0x0a is just a line feed, and Unix' idea of what a line break is.

    Splitting on m/\x0A/ works fine for me.

Re: Matching a hex value in a string...
by ysth (Canon) on Feb 27, 2008 at 11:15 UTC
    hex '0a' returns the number 10 (= 0x0a). To turn that into character 10, use chr(hex('0a')). But using the \x escape is easier: split /\x0a/. Which is identical to split /\n/, since your special character is just a newline.
      ... but on Windows "\n" is the same as "\r\x0A", isn't it?

      Or am I completely wrong here? (no windows perl installed, can't test it :()

        No. \n is \x0A on Windows. (And $/ is \n, since the CRLF→LF conversion occurs before the buffer is searched for the IRS.)

        >perl -e"print length qq{\n} 1 >perl -e"print qq{\n} eq qq{\x0A} ?1:0 1 >perl -e"print $/ eq qq{\x0A} ?1:0 1 >echo foo| perl -e"print length <> 4

        Maybe you're thinking of older Macs, where \n is \x0D.

        Update: Added proof.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://670573]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-06-24 22:49 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (562 votes). Check out past polls.