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

halo Monks, got another question: Is it possible to read an input file into either a string OR and array depending on or based off of pattern matching? To clarify:
___INPUT____
1. This string can be modified using only regexes
-These strings
-Need to be
-put into an
-array
2.Here's another string needs only regexes
-But here are more
-strings which 
-I want arrayed
so on and so forth...my code thus far,
open(TXTIN,"input") || die "Cannot open the data file"; open(TXTOUT,">output") || die "Cannot open the formatted file"; while(<TXTIN>){ if (/^[1-9]/){ s/^[1-9].//; print TXTOUT; } else{ @array=$_;#???this is where I am lost print TXTOUT @array;} } close (TXTIN); close (TXTOUT);
Also, I need the array to be 'refreshed' after each iteration(?)/string which doesn't need to arrayed. Sorry if this is too confusing, but I myself am confused now. Also, I know that each input string will be only one line, that is, contain only one '/n'.

Replies are listed 'Best First'.
Re: Input to String OR Array
by fishbot_v2 (Chaplain) on Oct 18, 2005 at 20:58 UTC

    Why do the lines need to be in an array? If you are confused, start by stating your goal, and forget the implementation for a moment. If we understand what you want to do, then we can help you.

    push @array, $_;

    might be the answer to your question, but why read into an array just to print it again? Why not just print line-by-line?

Re: Input to String OR Array
by Roy Johnson (Monsignor) on Oct 18, 2005 at 21:00 UTC
    else { push @array, $_; print "Array'd: $_"; }

    Caution: Contents may have been coded under pressure.
Re: Input to String OR Array
by GrandFather (Saint) on Oct 18, 2005 at 21:08 UTC

    This may be what you are looking for:

    use strict; use warnings; my $heading = ''; my @lines; while (my $line = <DATA>) { chomp $line; if ($line =~ /^[1-9]/) {#New heading line ProcessEntry ($heading, @lines) if length $heading; $heading = $line; @lines = (); } else {push @lines, $line;} } ProcessEntry ($heading, @lines) if length $heading; sub ProcessEntry { my $heading = shift; my @lines = @_; print "Heading is: $heading\n"; print "Lines are:\n " . join "\n ", @lines; print "\n\n"; }

    Which prints:

    Heading is: 1. This string can be modified using only regexes Lines are: -These strings -Need to be -put into an -array Heading is: 2.Here's another string needs only regexes Lines are: -But here are more -strings which -I want arrayed

    Perl is Huffman encoded by design.
      First off, I am using the Text::Wrap module on the first string to format the text the way it needs to be. So it takes the first long string and formats it beautifully, no problems here.
      After that, I need an array on the subsequent string(s) because the first string needs a special regex, the ones in the middle need a special regex, and the last string (before the next formatted string) needs yet another regex. The only way I know to accomplish this is to put those strings into an array. Sorry I tried to simplify the question, but may have lost the context along the way. Does this clarify? Suggestions? Thanks in advance, Joe

        So as sample code the sub becomes:

        sub ProcessEntry { my $heading = shift; my @lines = @_; print "Heading is: $heading\n"; print "middle lines are:\n"; print " $lines[$_]\n" for (0..(@lines-2)); print "Last line is: $lines[-1]\n\n"; }

        Which prints:

        Heading is: 1. This string can be modified using only regexes middle lines are: -These strings -Need to be -put into an Last line is: -array Heading is: 2.Here's another string needs only regexes middle lines are: -But here are more -strings which Last line is: -I want arrayed

        Obviously you can replace the print lines with whatever you require, but that should suffice to show you how to access the various lines.


        Perl is Huffman encoded by design.
Re: Input to String OR Array
by eyepopslikeamosquito (Archbishop) on Oct 18, 2005 at 21:05 UTC

    What you want to do seems unusual to me: in my experience, one normally reads a file into a string or an array. Most problems can be fully solved with either a string representation or an array one -- which representation is more appropriate depends on the problem. Also, it is easy to slurp the whole file into a string, then convert into an array of lines; for example:

    open(my $fh, '<', 'input') or die "open input: $!"; # Slurp file contents into a string. my $contents = do { local $/; <$fh> }; # Convert into an array. my @lines = split /^/, $contents;

    Can you describe what you are trying to achieve? Then we might be able to suggest solutions using either a string or an array (rather than both).