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

Sorry about the horrible title, I couldn't help it.

So I am a newbie, and I know that this question must have been asked and answered before, but I cannot seem to construct a query to find it:

I want to read a file, and search for a pre-defined delimeter string (in this case "%"). I want to start getting data at that point and stuffing into an array as one item, until it comes to another "%", at which point it should start stuffing the data into the next item in the array. Basically you can think of what I'm doing as a random quote generator, but I want to go slightly elsewhere with it eventually, I'm just stuck here.

So at the moment I can open and close the file, and find the "%", but I don't know how to say "start shoving into @array until you find another '%'". I could post my code if anyone wants to see it, but I suspect at this point that would be more embarrassing than helpful.

Any help would be appreciated, TIA.
Also... should I have put this in categorized q&a?
--pvck

Replies are listed 'Best First'.
Re: from $string to shining $string
by gellyfish (Monsignor) on Mar 21, 2002 at 14:31 UTC

    You can achieve this by setting the variable $/ to '%' before reading in the contents of the file:

    local $/ = '%'; my @contents = <FILE>; # each element is the percent separated bit

    /J\

Re: from $string to shining $string
by AidanLee (Chaplain) on Mar 21, 2002 at 14:32 UTC
    The easiest way is to locally re-set your record separator. Normally this is set to newline, but you can set it to anything you like:
    my @quotes = (); if( open(FILE,$myfile) ) { local $/ = '%'; @quotes = <FILE>; close FILE; } else { print "oops, i need to check why the file didn't open..."; }

      print "oops, i need to check why the file didn't open...";

      Of course if you had printed the value of $! you wouldn't need to check why ;-}

      /J\

Re: from $string to shining $string
by dragonchild (Archbishop) on Mar 21, 2002 at 15:34 UTC
    Of course, this is the same as saying:
    my $string; { local $/ = undef; $string = <FILE>; } my @stuff = split '%', $string;
    Now, a few things to note:
    1. Newlines will be preserved within the string. This may not be what you want.
    2. This will create null-strings wherever you have more than one '%' next to each other. This also may not be what you want.
    Assuming you don't want those two situations, I'd do something like:
    my $string; { local $/ = undef; $string = <FILE>; } $string =~ s/$///g; my @stuff = split /%+/, $string;

    Update: Changed $/=''; to $/ = undef;. The first is "paragraph mode". The second is "slurp mode". Thanx to tye who pointed that out! :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: from $string to shining $string
by pvck (Novice) on Feb 02, 2003 at 19:56 UTC
    I realize that it is very, very late to add this, but I lost all connectivity for some time after asking this question.
    All of these suggestions were very helpful, thank you everyone who contributed. If anyone still cares, I wound up using the $/ = % bit and it worked a charm.

    --puck