Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: About $/

by Athanasius (Chancellor)
on May 24, 2013 at 09:59 UTC ( #1035100=note: print w/replies, xml ) Need Help??


in reply to About $/

No, because, as perlvar says, “the value of $/ is a string, not a regex.” — and a string can hold only one value at a time.

Perhaps you can proceed in this way: Pick one of the two end-line strings (say, \r) and assign it to $/. Then, split each line on the other end-line character (\003) and process each string as a “line.”

Or, if your file isn’t too big, you can slurp it all into memory and then split using a regex:

19:55 >perl -MData::Dump -wE "my $s = qq[fred\rwilma\003barney\rbetty\ +003]; my @lines = split /\r|\003/, $s; dd @lines;" ("fred", "wilma", "barney", "betty") 19:56 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: About $/
by FloydATC (Deacon) on May 24, 2013 at 10:52 UTC
    If all else fails, use sysread() to implement your own read buffering and split the incoming data on the fly.

    open(my $fh, '/my/huge/file') || die $!; binmode $fh; my $separator = "\r*\n|\t|whatever"; my $buffer = ''; my $block; while (sysread($fh, $block, 4096)) { $buffer .= $block; while ($buffer =~ /$separator/) { process_line($`.$&); # prematch + match $buffer = $'; # postmatch } } print "--\n"; process_line($buffer) if $buffer; # remainder, if any. close $fh;

    This gives you full control over how to split the data into lines, without significant memory overhead.

    -- Time flies when you don't know what you're doing

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1035100]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2018-02-23 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (310 votes). Check out past polls.

    Notices?