Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create and use a byte array in perl?

Tags:

arrays

perl

$var = pack "C2", 0x20, 0x30;

seems to work well enough, but now how do I address the elements of the array? Make changes to elements? In-place if possible. The pack/unpack semantics are not very friendly.

Currently I'm using substr($var, $index, 1, substr($var, $index, 1) + 10) to add 10 to elements in-place.

And for intializers, if I need a 100 byte array of 0x20, what's the best way? $var = "\x20" x 100 works, is it the 'right' way?

like image 328
davenpcj Avatar asked Dec 30 '22 20:12

davenpcj


1 Answers

two questions, two answers:

Q. seems to work well enough, but now how do I address the elements of the array?

A. vec() is your friend:

vec($var, $index, 8) += 10;

will do what you want.

Q. for intializers, if I need a 100 byte array of 0x20, what's the best way? $var = "\x20" x 100 works, is it the 'right' way?

A. it's OK in my book.

like image 146
Massa Avatar answered Jan 07 '23 19:01

Massa