Perl record separator -
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
You said in your question the record separator is0x1e
, but your code is using0xa
(newline). Which is it?
– Schwern
Nov 20 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 at 4:43
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
perl parsing text-processing
asked Nov 20 at 4:24
prabhu
4591925
4591925
You said in your question the record separator is0x1e
, but your code is using0xa
(newline). Which is it?
– Schwern
Nov 20 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 at 4:43
add a comment |
You said in your question the record separator is0x1e
, but your code is using0xa
(newline). Which is it?
– Schwern
Nov 20 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 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 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 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 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 at 4:43
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
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);
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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);
}
add a comment |
up vote
3
down vote
accepted
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);
}
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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);
}
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);
}
answered Nov 20 at 4:40
Schwern
87.6k16101229
87.6k16101229
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You said in your question the record separator is
0x1e
, but your code is using0xa
(newline). Which is it?– Schwern
Nov 20 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 at 4:43