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

Hi, I am trying to split a file: #input load of stuff and something else #end trying split using the #input upto #end, is this possible? e.g
split(/^#input$^#end$/m,$_)

Replies are listed 'Best First'.
Re: split question
by davido (Cardinal) on Jul 02, 2004 at 15:34 UTC
    Do you mean something like this?
    my @loads_of_stuff; while ( my $line = <DATA> ) { chomp $line; while ( $line =~ m/#input(.+?)#end/g ) { push @loads_of_stuff, $1; } }

    This will run into trouble if #input and #end are potentially on different lines in the file. If that's a possibility, you my need the range operator, or to slurp chunksof the file, or even to set the file input record separator to #end.

    You will run into additional trouble if #input and #end are legal characters if placed within quotes, ie, #input "this is what you want to capture, including #end" #end


    Dave

Re: split question
by Grygonos (Chaplain) on Jul 02, 2004 at 14:53 UTC
    If there is a #end marker for example: #this -data1-data2-#end #this -data3-data4-#end you could simply split on #end,but doesn't seem like the correct methodolgy. Split (IMHO) relies on a separator, by having a beginning and end of field identifier, it seems something more suited to putting each record on its own line, and doing a while(<SOANDSO>){ } loop rather than putting it all on one line and splitting it up. If you want to split each line into its own element of an array, you could slurp the file (provided its not too huge) or use Tie::File
      i only thought of using a split function simply because i can not think of another method to use that will simple delete:
      #input xxx ... ... #end
      delete by finding /end/ and the 4 lines, delete whole entry split was an idea?