Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: parsing question

by smist (Acolyte)
on Dec 07, 2006 at 13:44 UTC ( #588314=note: print w/replies, xml ) Need Help??

in reply to Re: parsing question
in thread parsing question

Could someone please explain the following line? I haven't been able to figure out what the split function does here.
my @sections = split '-{103}', do{ local $/; <> };

Replies are listed 'Best First'.
Re^3: parsing question
by joeface (Pilgrim) on Dec 07, 2006 at 16:20 UTC

    This sets the default line-separator (held in "$/", normally set to "\n") to undef, so that whatever comes in from STDIN (<>) will get slurped up, split on the string "-{103}", and assigned to @sections.

    A short example... put the following into a script named "":

    #!/usr/bin/perl -w use strict; my @sections = split 'FOO', do {local $/; <>}; close *ARGV; print join("\n", @sections);

    Then, put the following into a file called "test.txt":

    FOObarFOObarFOObarFOObarFOObar FOObarFOObarFOObarFOObarFOObar FOObarFOObarFOObarFOObarFOObar

    Then, run the following at the command-line:

    cat test.txt | ./
      Thanks for the reply. I'm not seeing where "-{103}" is, in the text file I'm parsing.

      UPDATED: Nevermind the above. :-) I'm such a newbie. I didn't recognize the {} as being quantifiers in a regex. So, "-{103}" is simply the line of hyphens used to separate the sections in the text file.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588314]
[Your Mother]: …how on Earth could Perl’s grep recurse?
[Tanktalus]: perldigious: I'm not looking to get rich. I'm looking for interesting work, with good continuity of employment, and maintenance of our current lifestyle. (An improvement wouldn't be bad, either, but that's not a requirement ;) )
erix enjoys the confusion++
[Your Mother]: :P
[Tanktalus]: Without confusion, I'd be out of a job ;)
[Tanktalus]: Already. :D
[Lady_Aleena]: Your Mother, I wish it did have a way to put in a directory to search.
[Your Mother]: That’s silly. It’s a string op tool.
[Lady_Aleena]: So I guess I'll go argue with File::Find and grep.
[Your Mother]: I would probably use Path::Tiny instead for it's slurp and such.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2017-05-23 19:35 GMT
Find Nodes?
    Voting Booth?