Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!convex!usenet From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: better head Message-ID: <1991May17.172231.21193@convex.com> Date: 17 May 91 17:22:31 GMT References: <1991May16.152356.3186@convex.com> <1991May16.205446.15295@uunet.uu.net> Sender: usenet@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 50 Nntp-Posting-Host: pixel.convex.com From the keyboard of rbj@uunet.uu.net (Root Boy Jim): :> unshift(@ARGV, '-') unless $argc = @ARGV; # <> changes @ARGV :> while (<>) { :> if ($. == 1 && $argc > 1) { :> print "\n" if $deja_imprime++; : :It's deja vu all over again :-) vraiment. :-) :> if ($. <= $num) { :> print; :> } else { :> close ARGV; :> } : :I think you're reading too many lines. :Wouldn't "print; close ARGV if $. == $num" be better? I may be *reading* one line "too many", but at least the output is still right. :I think you missed something tho. :What happens if some of the files have less than $num lines? It's true. This works for those cases, too: #!/usr/bin/perl # # head -- perl clone of head command, but without head's silly # limits regarding line lengths. tchrist@convex.com $num = ($ARGV[0] =~ /^-(.+)/ && shift) ? $1 : 10; die "$0: badly formed number: $1\n" unless $num =~ /^\d+$/; unshift(@ARGV, '-') unless $argc = @ARGV; # <> changes @ARGV while (<>) { if ($. == 1 && $argc > 1) { print "\n" if $deja_imprime++; print "=> $ARGV <=\n" ; } print; close ARGV if $. == $num || eof; } --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."