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

Hi, I have a file containing the following information:
>text 4 'As the low pressure moves in, the winds should slacken towards the en +d of the day. Will be a smaller day than Tuesday, but the odd clean w +ave should be found at some spots.' >text 8 'Unsure as to what the winds will do more today as it'll depend a lot +on the track of the low pressure.' >text 9 'It'll be near calm and mellow conditions, although there's a chance o +f perhaps the odd kneehigh set wave for the North coast.'
I'd like to input this information into my program and split the file into seperate array elements. One element in the array will contain one header with its following sentence. So, in this example the array will be 3 elments in size. Thanks

Replies are listed 'Best First'.
Re: seperating blocks of file content in an array
by BrowserUk (Patriarch) on Nov 18, 2002 at 12:42 UTC

    Not sure if this is what you really want, but it is what you asked for. Making it read from an external file is left as AEFTR.

    #! perl -slw use strict; my @array; while(my $firstLine = <DATA>) { chomp $firstLine; push @array, $firstLine . ':'. <DATA>; } print for @array; __DATA__ >text 4 'As the low pressure moves in, the winds should slacken towards the en +d of the day. Will be a smaller day than Tuesday, but the odd clean w +ave should be found at some spots.' >text 8 'Unsure as to what the winds will do more today as it'll depend a lot +on the track of the low pressure.' >text 9 'It'll be near calm and mellow conditions, although there's a chance o +f perhaps the odd kneehigh set wave for the North coast.'

    However, if you gave us a clearer idea of how you are going to use your "array", and if you posted some code to show what problems you are having, it would probably be possible to suggest a better solution.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: seperating blocks of file content in an array
by Aristotle (Chancellor) on Nov 18, 2002 at 12:37 UTC
    Assuming the "text" lines are the headers and always look that way:
    my @sentence = do { local $/; open my $fh, "<", $filename; split /(?=^>text \d+$)/m, <$fh>; };

    Makeshifts last the longest.

Re: seperating blocks of file content in an array
by LTjake (Prior) on Nov 18, 2002 at 12:46 UTC
    So, did you try anything? I don't see any code -- tsk tsk.

    (Note: the following code is ugly. :)

    This one just combines every two lines and puts it into an array slot.
    use strict; my $i = 0; my $j = 0; my @result; while(<DATA>) { chomp; $result[(++$i % 2) ? $j++ : $j - 1] .= $_; } __DATA__ >text 4 'As the low pressure moves in, the winds should slacken towards the en +d of the day. Will be a smaller day than Tuesday, but the odd clean w +ave should be found at some spots.' >text 8 'Unsure as to what the winds will do more today as it'll depend a lot +on the track of the low pressure.' >text 9 'It'll be near calm and mellow conditions, although there's a chance o +f perhaps the odd kneehigh set wave for the North coast.'
    This next one creates an array of hashrefs with keys "header" and "text" to store the data.
    use strict; my @data = <DATA>; my @result; chomp foreach @data; for (my $i = 0; $i <= $#data; $i += 2) { push(@result, {header => $data[$i], text => $data[$i + 1]}); } __DATA__ >text 4 'As the low pressure moves in, the winds should slacken towards the en +d of the day. Will be a smaller day than Tuesday, but the odd clean w +ave should be found at some spots.' >text 8 'Unsure as to what the winds will do more today as it'll depend a lot +on the track of the low pressure.' >text 9 'It'll be near calm and mellow conditions, although there's a chance o +f perhaps the odd kneehigh set wave for the North coast.'


    --
    Rock is dead. Long live paper and scissors!