in reply to Concatenate records

Your "specification" is a bit vague and the exercise seems rather light, but here's a sketch to give you the flavour of Perl:

while (<>) { my ($prefix, $tail) = /(\S+) (.*)/; print "\n" if $currPrefix && $currPrefix ne $prefix; print "$prefix $tail"; $currPrefix = $prefix; }

which works without change for both redirected input and input files with output to stdout in both cases. However a little more work is required to correctly handle the perl proc.pl infile outfile variant:

#!/usr/bin/perl use strict; use warnings; my $in = *STDIN; my $out = *STDOUT; if (@ARGV == 2) { open $in, '<', $ARGV[0] or die "Can't open '$ARGV[0]': $!\n"; open $out, '>', $ARGV[1] or die "Can't create '$ARGV[1]': $!\n"; } my $currPrefix; while (<$in>) { my ($prefix, $tail) = /(\S+) (.*)/; print $out "\n" if $currPrefix && $currPrefix ne $prefix; print $out "$prefix $tail"; $currPrefix = $prefix; }

which works as expected using redirection or infile/outfile.

An interesting variant is:

#!/usr/bin/perl use strict; use warnings; my $currPrefix; $^I = '.bak'; while (<>) { my ($prefix, $tail) = /(\S+) (.*)/; print "\n" if $currPrefix && $currPrefix ne $prefix; print "$prefix $tail"; $currPrefix = $prefix; }

which will in place edit a list of input files generating backups of the original files with '.bak' extensions.

However, if you are dealing with databases then you are heading into territory where Perl shines so if you like, ask a similar question involving databases.

Perl is the programming world's equivalent of English