CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [vishwakar] Have to exclude the first and last line of the file : help me: Edit Log



FishMonger
Veteran / Moderator

Dec 28, 2010, 6:13 AM


Views: 3840
Re: [vishwakar] Have to exclude the first and last line of the file : help me


In Reply To
this was ur logic i was using .. to get the output..

Code
#!/usr/bin/perl 
use File::Copy;
use IO::File;
open my $inputfh, '<',"latestfile" or die "Failed to open latestfile $!";
my @lines = <$inputfh>;
close $inputfh;

#Remove first and last lines
my @test =shift @lines;
my @last =pop @test;


#Print lines
foreach my $last (@last) {
my @cells = split /\|/, $last;
print "@cells";
}

this was the output

$ perl text.pl
789 ADD 10
790 ADD 20
791 CHANGE 30
791 DISCONNECT 10
792 ADD 15
TRL 000000005


it was not removing the last line


That script won't produce the output you claim, unless you change this:

Code
foreach my $last (@last) {

to this:

Code
foreach my $last (@lines) {


Never retype test code in a post. Always copy/paste your actual code.

Here's an example script showing the approach I'd take.

Code
#!/usr/bin/perl 

use strict;
use warnings;

<DATA>; # throw away the first line

while ( <DATA> ) {
last if eof; # throw away the last line
tr /|/ /;
print;
}


__DATA__
HEADER|234
421|ADD|345
422|INSERT|567
421|INSERT|588
445|MULTIPLY|596
TAILER|234


Here's the results:

Quote
E:\TEMP\test>test.pl
421 ADD 345
422 INSERT 567
421 INSERT 588
445 MULTIPLY 596



(This post was edited by FishMonger on Dec 28, 2010, 6:14 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Dec 28, 2010, 6:14 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives