Hi Dennis.

Dennis Warren wrote:
> Hi everyone,
>
> I'm a hopeless hacker of Perl, and I tend to think in AWK-ish ways, simply
> because I know awk loads better than Perl.

What do you mean by 'awk loads better'? Beware that my Unix isn't
especially strong!

> Awk has unfortunately taught me to think purely in terms of fields...
>
> If I have a text file where the field separator is a new line and the record
> separator is a blank line, like this:
>
> blah blah blah
> yeah yeah yeah
>
> blah blah blah
> yeah yeah yeah
>
> etc...
>
> is there a simple way to slurp all the first fields into one array and all
> the second fields into another array.
> I know how to do this with a simple text file with just one array, i.e.
>
> open FILE1, $ARGV[0] or die "Can't open $ARGV[0]: $!\n";
> chomp(@input = <FILE1>);
>
> but can I simply change the second line in such a way as to split the input
> into two separate arrays?
> would I use split to do this?

If your input filename is on the command line (and therefore appears
in @ARGV) you can read from it with <> without having to explicitly
open it:

    chomp @input = <>

is your complete program. The following will do what you want.
It works by setting the input record separator to paragraph mode
by setting it to the null string. Note that I've printed out the second
array first to prove that it's doing something.

I hope this helps,

Rob


    local $/ = '';
    @array1 = split /\n/, <DATA>;
    @array2 = split /\n/, <DATA>;

    print ">>$_\n<<" foreach @array2;
    print "\n";
    print ">>$_\n<<" foreach @array1;

    __DATA__
    blah blah blah
    yeah yeah yeah

    BLAH BLAH BLAH
    YEAH YEAH YEAH

output

    >>BLAH BLAH BLAH<<
    >>YEAH YEAH YEAH<<

    >>blah blah blah<<
    >>yeah yeah yeah<<





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to