Note that there are some explanatory texts on larger screens.

plurals
  1. PORegarding perl unpack
    primarykey
    data
    text
    <p>I am planning to use unpack in the file. First i tested with a string. When i have a embedded space in a string the below script is showing it as null. When i tested with a file space is getting read properly. Not sure why it's changing it to null when i do with string. Can i use unpack while reading a fixed length file? Do i need to consider trailing space or anything else?</p> <pre><code>#!/usr/local/bin/perl use strict; use warnings; my $str="hek kaaa"; print "&lt;$str&gt;\n"; foreach(unpack("(A1)*", $str)) { print sprintf("%x", ord), " "; } Output: &lt;hek kaaa&gt; 68 65 6b 0 6b 61 61 61 </code></pre> <p>Thanks a lot for the response. Pasted my query below </p> <p>When the perl program reads from a text file and unpacks using the format "A" is working properly. First line i have a embedded space in the first field between A and D. LABEL Variable gets printed as "A D3". But when i had a variable $str="A D3" and unpacked the below way it's having a null after A. How is it working differently when reading from a file and variable? </p> <pre><code>foreach(unpack("(A1)*", $str)) { print sprintf("%x", ord), " "; } </code></pre> <p>it displays as the hex output as 41 0 44 33</p> <pre><code>cat test.txt A D37845566974923342XYZ24023984 QRW49327408234028434ERD24448009 my $file = 'test.txt'; open(my $data, '&lt;', $file) or die "Could not open '$file'\n"; while (my $line = &lt;$data&gt;) { print $line; chomp $line; my ($label, $source, $dest, $type, $value) = unpack ("A4 A8 A8 A4 A8", $line); print "LABEL: $label SOURCE: $source DEST: $dest TYPE: $type VALUE: $value\n"; print "length of a string:" . length($line) . "\n"; foreach(unpack("(a1)*", $label)) { print sprintf("%x", ord), " "; } print "\n"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload