Dennis Warren wrote:
> Thanks Rob, that helps me.
>
> > What do you mean by 'awk loads better'?
> I mean that I know awk better than I know Perl.
> In this context 'loads' means 'a lot', rather than transfer into memory or
> any other such computerese.

Ah! Sorry - I guess I expected the other leaming in a Perl newsgroup,
i.e. you knew that it loaded better.

> BTW I found I could also do it in the following way the following, but I
> knew in my heart that it was very ugly and clumsy.

This is nearly the same as my solution, except that it does what
you want! I've just reread your original post, and I realise that I
split your data up the wrong way. I'll go through your solution and
then fix mine...

You should 'use strict' for everyhting, even trivial programs.
'use warnings' is also useful.

    use strict;

> open FILE1, $ARGV[0] or die "Can't open $ARGV[0]: $!\n";

No need to do your own open if the file is specified on the
command line. Just read from <>, the null filehandle.

> $FS = "\n";

You can do this if you want, but you might as well code the constant
straight into the function call. Also, uppercase names are usually
reserved for filehandles and constants (which I guess this is, but a
variable one!). You would need to declare $fs with 'my' if you had
'use strict':

    my $fs = "\n";

> $/ = "\n\n";

This is almost the same as setting to the empty string, except that
this will start the next record after exactly two newlines, whereas
the latter will treat any number of consecutive blank lines as a
record separator.

> while (<FILE1>) {
> chomp
>     ($Fld1, $Fld2) = split($FS, $_, 9999);

No need to use the third parameter of 'split'. It will split at all
occurrences of the separator until if you omit it. (Actually it will
only split enough times to fill the variables before the '=', i.e.
twice here.) This parameter is used for limiting the number of
fields, so that

    split /:/, $header, 2;

would separate an header label from its value, even if there
were further colons in the value.

Also, 'split' takes $_ as its second parameter by default, so you
could write

    split /:/;

with the same result.

>    push(@input,$Fld1);
>     push(@input2,$Fld2);
>   }

This is where we differ, and you're right while I'm wrong. Here's
my rewrite. Note that the read from <DATA> will be replaced by
a read from <> if the file is specified on the command line. Also,
the call to 'split' will have an implied third parameter of 3, to
split the data into $f1, $f2 and 'the rest' which is thrown away.

Hope I got it right this time :-)

Rob


    use strict;

    local $/ = '';

    my ( @array1, @array2);

    while (<DATA>) {
        my ($f1, $f2) = split /\n/;
        push @array1, $f1;
        push @array2, $f2;
    }

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

    __DATA__
    blah blah blah
    yeah yeah yeah

    BLAH BLAH BLAH
    YEAH YEAH YEAH

output

    >>yeah yeah yeah<<
    >>YEAH YEAH YEAH<<

    >>blah blah blah<<
    >>BLAH BLAH BLAH<<





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

Reply via email to