Hello PerlMonks,
From a device I get binary data, and in this data I need to replace a (binary sub-)string by another one. I thought that I could simply read the data into a string and hoped that s/$cut/$place/g would do the trick. No such luck.
Can it be done using Perl? If so: how? The length of $cut and $place are identical, the location is unknown.
Thanksupdate, made a variation on my original testcode to see if I could find the index:
Update 2: made new testdata, and the code works! Thanks!my $datafile=<file with binary data>; my $searchdata=<file with string to be replaced>; my ($FH,$buf, $cut); binmode($FH); open ($FH, $datafile); read($FH, $buf, 10000); chop($buf); close ($FH); open ($FH,$searchdata ); read($FH, $cut, 100); chop($cut); close($FH); my $result = index($buf,$cut); print "position: $result\n";
In reply to binary search/replace by momo33
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |