Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Creating data delimited by ASCII code 1 using perl

by dasun (Initiate)
on Jun 21, 2019 at 07:50 UTC ( [id://11101656]=perlquestion: print w/replies, xml ) Need Help??

dasun has asked for the wisdom of the Perl Monks concerning the following question:

Any suggestion how to create a file where the values are separated by ASCII code 1 (binary1),with data extracted from a table using shell script The format is :

columnname1(binary1)columnvalue(binary1)columnname2(binary1)columnvalue(binary1)columnname3(binary1)columnvalue... 1st row columnname1(binary1)columnvalue(binary1)columnname2(binary1)columnvalue(binary1)columnname3(binary1)columnvalue.. second row
  • Comment on Creating data delimited by ASCII code 1 using perl

Replies are listed 'Best First'.
Re: Creating data delimited by ASCII code 1 using perl
by haukex (Archbishop) on Jun 21, 2019 at 08:06 UTC
    Any suggestion how to create a file where the values are separated by ASCII code 1 (binary1)

    In Perl, that can be represented in a double-quoted string as "\x01" or "\001" (among others). As for the rest of your question, please see How do I post a question effectively?.

Re: Creating data delimited by ASCII code 1 using perl
by johngg (Canon) on Jun 21, 2019 at 09:47 UTC

    It is not clear how you are getting your data but if you hold the column names in a separate array from the data you can use List::MoreUtils->mesh() to combine the two, joining each element with the delimiter \x01.

    johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MList::MoreUtils +=mesh -E ' my $delim = qq{\x01}; my @colHdrs = qw{ Name Sex Age Job }; my @data = ( [ qw{ Fred M 27 Janitor } ], [ qw{ Mary F 31 Researcher } ], ); my $outFile = q{spw11101656.out}; open my $outFH, q{>}, $outFile or die qq{open: > $outFile: $!\n}; foreach my $raDataLine ( @data ) { say $outFH join $delim, mesh @colHdrs, @{ $raDataLine }; } close $outFH or die qq{close: > $outFile: $!\n};' johngg@shiraz:~/perl/Monks$ hexdump -C spw11101656.out 00000000 4e 61 6d 65 01 46 72 65 64 01 53 65 78 01 4d 01 |Name.Fred +.Sex.M.| 00000010 41 67 65 01 32 37 01 4a 6f 62 01 4a 61 6e 69 74 |Age.27.Jo +b.Janit| 00000020 6f 72 0a 4e 61 6d 65 01 4d 61 72 79 01 53 65 78 |or.Name.M +ary.Sex| 00000030 01 46 01 41 67 65 01 33 31 01 4a 6f 62 01 52 65 |.F.Age.31 +.Job.Re| 00000040 73 65 61 72 63 68 65 72 0a |searcher. +| 00000049

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Creating data delimited by ASCII code 1 using perl
by shmem (Chancellor) on Jun 21, 2019 at 12:07 UTC
    ASCII code 1 (binary1),

    ASCII code 1:

    $\ = "\n"; # output record separator print "\x01"; # hex print "\001"; # octal print "\cA"; # control char print pack "C",1; # byte with the lowest bit set

    You can use any of them with join and split.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      TIMTOWTDI Party? ;-)

      print "\x{0001}"; print "\N{U+1}"; print "\N{START OF HEADING}"; print chr(1); print v1; print "x"^"y"; vec(my $x,0,8)=1; print $x;

      Update: "x"^"y" only works if the new bitwise feature is off; it could be turned on implicitly by the -E switch or a use 5.028; or higher (thanks for pointing this out, davido!). When the bitwise feature is on, one has to use the new ^. operator instead. Also, \N{START OF HEADING} requires charnames to be loaded explicitly on Perls before 5.16.

Re: Creating data delimited by ASCII code 1 using perl
by ikegami (Patriarch) on Jun 21, 2019 at 20:56 UTC
    use Encode qw( encode ); open(my $fh, '>:raw', $qfn) or die("Can't create \"$qfn\": $!\n"); print($fh join("\x01", map encode($encoding, $_), @values));

    Of course, in most encodings (and UTF-8 in particular), U+0001 encodes as 01, so you can use an :encoding layer as normal.

    use open ':encoding(UTF-8)'; open(my $fh, '>', $qfn) or die("Can't create \"$qfn\": $!\n"); print($fh join("\x01", @values);

    Finally, if the values are terminated by 01 rather than separated by 01, you'd use

    use open ':encoding(UTF-8)'; open(my $fh, '>', $qfn) or die("Can't create \"$qfn\": $!\n"); print($fh "$_\x01") for @values;

    or

    use open ':encoding(UTF-8)'; open(my $fh, '>', $qfn) or die("Can't create \"$qfn\": $!\n"); local $\ = "\x01"; print($fh $_) for @values;

    The last one might be tempting, but the global nature of the change to $\ could negatively affect code in modules.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11101656]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found