in reply to Write 2 uint16_t numbers to file in Perl

The critical point to remember is that
    $rgb[16]=pack("S", 7360);  # Overwrite 3689 with 7360
does not assign 7360 to  $rgb[16] but instead assigns a two-byte string that is the S-packed representation of 7360. After the pair of assignments in your OPed code,  @rgb contains a Hellish mix of numbers and strings. Don't do that. Instead, just assign numbers to the array elements and re-pack:

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my @ra = (1, 3689, 2, 2462, 3); dd \@ra; ;; my $pS1 = pack 'S*', @ra; dd $pS1; print 'length $pS1 == ', length $pS1; ;; my @rb = unpack 'S*', $pS1; dd \@rb; ;; $rb[1] = 7360; $rb[3] = 4912; my $pS2 = pack 'S*', @rb; dd $pS2; print 'length $pS2 == ', length $pS2; ;; my @rc = unpack 'S*', $pS2; dd \@rc; " [1, 3689, 2, 2462, 3] "\1\0i\16\2\0\x9E\t\3\0" length $pS1 == 10 [1, 3689, 2, 2462, 3] "\1\0\xC0\34\2\x000\23\3\0" length $pS2 == 10 [1, 7360, 2, 4912, 3]


Give a man a fish:  <%-{-{-{-<