in reply to Variable number of words/fields in a line/record

You don't need all those separate variables. Try using an array or a hash instead, for instance:
while (<IN>) # Note: using $_ instead { my (@two, @rest); chomp; @rest = split; # Note that this is the same as (split ' ', $_) . # ' ' is generally a better choice than /\s/ for sp +litting on # whitespace. If you need to count multiple spaces + as # multiple fields, use /\s/ or / / instead. @two = splice @rest, 0, 2; print join(' ',@two,undef),join('|',@rest),"\n"; # The undef is to + put another space in before @rest }
See also splice split join perlop.

Replies are listed 'Best First'.
Re: Re: Variable number of words/fields in a line/record
by premchai21 (Curate) on Jun 16, 2001 at 08:08 UTC
    Or (after chomp and before print):
    (@two[0..1], @rest) = split;