Perl record separator -












0















I'm stuck on a seemingly trivial problem but not sure what is it that I'm missing. Need help.



I have a file that is delimited by the standard field separator (0x1f) and record separator (0x1e) characters. (https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text)



I don't need to parse out the fields but interested in getting the records.



I read about Perl's record separator special variable and tried using that to parse the file.



The file looks like this. ^ represents the field separator and ^^ represents the record separator (in vim). On sublime these will show up as the relevant hex codes.



ID^_NAME^_PARENTID^_Prov ID^_Pat_ID^_Another ID^_Program1^_Program2^_Status^_Date^_Reason^_Added^_Sn Length^_ze Reason^_StAge^_EnAge^_Notes^^NUMBER^_VARCHAR^_NUMBER^_    NUMBER^_NUMBER^_NUMBER^_VARCHAR^_VARCHAR^_VARCHAR^_DATE^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^^12^_40^_12^_^_12^_12^_200^_200^_12^_^_200^_1^_    4000^_4000^_2000^_2000^_4000^^0^_^_0^_^_0^_0^_^_^_^_^_^_^_^_^_^_^_^^


Following is the code that I wrote to parse the records out. Issue is, whatever I do, the entire file is read into the $row scalar.



I initially assumed that perl expects the $/ to be set to a string type. Doing that also doesn't seem to work and I'm stuck.



Appreciate any help. Thanks.



#local $/ = sprintf("%s",chr("0xa"));
local $/ = chr(0xa);

open my $fh, "<", $file or die "$file: $!";

print("reading recordsn");

while (my $row = <$fh>) {
print("Record:", $row, "n");
}









share|improve this question























  • You said in your question the record separator is 0x1e, but your code is using 0xa (newline). Which is it?

    – Schwern
    Nov 20 '18 at 4:31








  • 1





    @Schwern Sorry about that. I was tweaking the code to see how this behaves with setting $/ with newline. Inadvertently posted that WIP code. Thanks for spotting.

    – prabhu
    Nov 20 '18 at 4:43
















0















I'm stuck on a seemingly trivial problem but not sure what is it that I'm missing. Need help.



I have a file that is delimited by the standard field separator (0x1f) and record separator (0x1e) characters. (https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text)



I don't need to parse out the fields but interested in getting the records.



I read about Perl's record separator special variable and tried using that to parse the file.



The file looks like this. ^ represents the field separator and ^^ represents the record separator (in vim). On sublime these will show up as the relevant hex codes.



ID^_NAME^_PARENTID^_Prov ID^_Pat_ID^_Another ID^_Program1^_Program2^_Status^_Date^_Reason^_Added^_Sn Length^_ze Reason^_StAge^_EnAge^_Notes^^NUMBER^_VARCHAR^_NUMBER^_    NUMBER^_NUMBER^_NUMBER^_VARCHAR^_VARCHAR^_VARCHAR^_DATE^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^^12^_40^_12^_^_12^_12^_200^_200^_12^_^_200^_1^_    4000^_4000^_2000^_2000^_4000^^0^_^_0^_^_0^_0^_^_^_^_^_^_^_^_^_^_^_^^


Following is the code that I wrote to parse the records out. Issue is, whatever I do, the entire file is read into the $row scalar.



I initially assumed that perl expects the $/ to be set to a string type. Doing that also doesn't seem to work and I'm stuck.



Appreciate any help. Thanks.



#local $/ = sprintf("%s",chr("0xa"));
local $/ = chr(0xa);

open my $fh, "<", $file or die "$file: $!";

print("reading recordsn");

while (my $row = <$fh>) {
print("Record:", $row, "n");
}









share|improve this question























  • You said in your question the record separator is 0x1e, but your code is using 0xa (newline). Which is it?

    – Schwern
    Nov 20 '18 at 4:31








  • 1





    @Schwern Sorry about that. I was tweaking the code to see how this behaves with setting $/ with newline. Inadvertently posted that WIP code. Thanks for spotting.

    – prabhu
    Nov 20 '18 at 4:43














0












0








0








I'm stuck on a seemingly trivial problem but not sure what is it that I'm missing. Need help.



I have a file that is delimited by the standard field separator (0x1f) and record separator (0x1e) characters. (https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text)



I don't need to parse out the fields but interested in getting the records.



I read about Perl's record separator special variable and tried using that to parse the file.



The file looks like this. ^ represents the field separator and ^^ represents the record separator (in vim). On sublime these will show up as the relevant hex codes.



ID^_NAME^_PARENTID^_Prov ID^_Pat_ID^_Another ID^_Program1^_Program2^_Status^_Date^_Reason^_Added^_Sn Length^_ze Reason^_StAge^_EnAge^_Notes^^NUMBER^_VARCHAR^_NUMBER^_    NUMBER^_NUMBER^_NUMBER^_VARCHAR^_VARCHAR^_VARCHAR^_DATE^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^^12^_40^_12^_^_12^_12^_200^_200^_12^_^_200^_1^_    4000^_4000^_2000^_2000^_4000^^0^_^_0^_^_0^_0^_^_^_^_^_^_^_^_^_^_^_^^


Following is the code that I wrote to parse the records out. Issue is, whatever I do, the entire file is read into the $row scalar.



I initially assumed that perl expects the $/ to be set to a string type. Doing that also doesn't seem to work and I'm stuck.



Appreciate any help. Thanks.



#local $/ = sprintf("%s",chr("0xa"));
local $/ = chr(0xa);

open my $fh, "<", $file or die "$file: $!";

print("reading recordsn");

while (my $row = <$fh>) {
print("Record:", $row, "n");
}









share|improve this question














I'm stuck on a seemingly trivial problem but not sure what is it that I'm missing. Need help.



I have a file that is delimited by the standard field separator (0x1f) and record separator (0x1e) characters. (https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text)



I don't need to parse out the fields but interested in getting the records.



I read about Perl's record separator special variable and tried using that to parse the file.



The file looks like this. ^ represents the field separator and ^^ represents the record separator (in vim). On sublime these will show up as the relevant hex codes.



ID^_NAME^_PARENTID^_Prov ID^_Pat_ID^_Another ID^_Program1^_Program2^_Status^_Date^_Reason^_Added^_Sn Length^_ze Reason^_StAge^_EnAge^_Notes^^NUMBER^_VARCHAR^_NUMBER^_    NUMBER^_NUMBER^_NUMBER^_VARCHAR^_VARCHAR^_VARCHAR^_DATE^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^^12^_40^_12^_^_12^_12^_200^_200^_12^_^_200^_1^_    4000^_4000^_2000^_2000^_4000^^0^_^_0^_^_0^_0^_^_^_^_^_^_^_^_^_^_^_^^


Following is the code that I wrote to parse the records out. Issue is, whatever I do, the entire file is read into the $row scalar.



I initially assumed that perl expects the $/ to be set to a string type. Doing that also doesn't seem to work and I'm stuck.



Appreciate any help. Thanks.



#local $/ = sprintf("%s",chr("0xa"));
local $/ = chr(0xa);

open my $fh, "<", $file or die "$file: $!";

print("reading recordsn");

while (my $row = <$fh>) {
print("Record:", $row, "n");
}






perl parsing text-processing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 4:24









prabhuprabhu

4591925




4591925













  • You said in your question the record separator is 0x1e, but your code is using 0xa (newline). Which is it?

    – Schwern
    Nov 20 '18 at 4:31








  • 1





    @Schwern Sorry about that. I was tweaking the code to see how this behaves with setting $/ with newline. Inadvertently posted that WIP code. Thanks for spotting.

    – prabhu
    Nov 20 '18 at 4:43



















  • You said in your question the record separator is 0x1e, but your code is using 0xa (newline). Which is it?

    – Schwern
    Nov 20 '18 at 4:31








  • 1





    @Schwern Sorry about that. I was tweaking the code to see how this behaves with setting $/ with newline. Inadvertently posted that WIP code. Thanks for spotting.

    – prabhu
    Nov 20 '18 at 4:43

















You said in your question the record separator is 0x1e, but your code is using 0xa (newline). Which is it?

– Schwern
Nov 20 '18 at 4:31







You said in your question the record separator is 0x1e, but your code is using 0xa (newline). Which is it?

– Schwern
Nov 20 '18 at 4:31






1




1





@Schwern Sorry about that. I was tweaking the code to see how this behaves with setting $/ with newline. Inadvertently posted that WIP code. Thanks for spotting.

– prabhu
Nov 20 '18 at 4:43





@Schwern Sorry about that. I was tweaking the code to see how this behaves with setting $/ with newline. Inadvertently posted that WIP code. Thanks for spotting.

– prabhu
Nov 20 '18 at 4:43












1 Answer
1






active

oldest

votes


















3














You can use chr(0xNN), but it's simpler to write a hex character as "xNN". A string containing record separator is "x1e".



#!/usr/bin/env perl

use strict;
use warnings;
use v5.10;

my $file = shift;
open my $fh, "<", $file or die "$file: $!";

say "reading records";

local $/ = "x1e";
while (my $row = <$fh>) {
say("Record:", join ",", split /x1f/, $row);
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386203%2fperl-record-separator%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You can use chr(0xNN), but it's simpler to write a hex character as "xNN". A string containing record separator is "x1e".



    #!/usr/bin/env perl

    use strict;
    use warnings;
    use v5.10;

    my $file = shift;
    open my $fh, "<", $file or die "$file: $!";

    say "reading records";

    local $/ = "x1e";
    while (my $row = <$fh>) {
    say("Record:", join ",", split /x1f/, $row);
    }





    share|improve this answer




























      3














      You can use chr(0xNN), but it's simpler to write a hex character as "xNN". A string containing record separator is "x1e".



      #!/usr/bin/env perl

      use strict;
      use warnings;
      use v5.10;

      my $file = shift;
      open my $fh, "<", $file or die "$file: $!";

      say "reading records";

      local $/ = "x1e";
      while (my $row = <$fh>) {
      say("Record:", join ",", split /x1f/, $row);
      }





      share|improve this answer


























        3












        3








        3







        You can use chr(0xNN), but it's simpler to write a hex character as "xNN". A string containing record separator is "x1e".



        #!/usr/bin/env perl

        use strict;
        use warnings;
        use v5.10;

        my $file = shift;
        open my $fh, "<", $file or die "$file: $!";

        say "reading records";

        local $/ = "x1e";
        while (my $row = <$fh>) {
        say("Record:", join ",", split /x1f/, $row);
        }





        share|improve this answer













        You can use chr(0xNN), but it's simpler to write a hex character as "xNN". A string containing record separator is "x1e".



        #!/usr/bin/env perl

        use strict;
        use warnings;
        use v5.10;

        my $file = shift;
        open my $fh, "<", $file or die "$file: $!";

        say "reading records";

        local $/ = "x1e";
        while (my $row = <$fh>) {
        say("Record:", join ",", split /x1f/, $row);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 4:40









        SchwernSchwern

        88.7k16101230




        88.7k16101230






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386203%2fperl-record-separator%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            Npm cannot find a required file even through it is in the searched directory

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith