momo33 has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: binary search/replace
by roboticus (Chancellor) on Jan 26, 2011 at 12:51 UTC | |
by momo33 (Beadle) on Jan 26, 2011 at 13:11 UTC | |
|
Re: binary search/replace
by tilly (Archbishop) on Jan 26, 2011 at 12:57 UTC | |
|
Re: binary search/replace
by nif (Sexton) on Jan 26, 2011 at 13:22 UTC | |
|
Re: binary search/replace
by bart (Canon) on Jan 26, 2011 at 13:37 UTC | |
|
Re: binary search/replace
by ambrus (Abbot) on Jan 26, 2011 at 13:58 UTC |