Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
discard the first line

Posted by Tim (Tim), 24 October 2007
Can anyone help me this??

i have read the whole input file into an array.
now i wanted to discard only the first line.
please tell me how to do this. i can retrieve the first line but don't know how to discard the same.

Thanks.

Posted by george_Ball (george), 24 October 2007
open(<IN>, "file");
<IN>;
my @rest_of_file = <IN>;

The first <IN> reads the first line and throws it away, the second call is in array context so reads all the remaining lines into the array.

Hope that's what you were meaning?

Posted by KevinAD (KevinAD), 25 October 2007
remove first element of an array:

shift @array;

But george has posted a method you can use to avoid shifting the array later.

Posted by admin (Graham Ellis), 25 October 2007
"There's more than one way of doing it"  

Thanks Kevin and George ... and neither of you mentioned using list slices ...

Posted by Tim (Tim), 25 October 2007
I tried but not working...
i have pasted the code.

use warnings;
print "Please enter the input file name\n";
 $file = <STDIN>;
 open(INFILE, $file);

foreach (<INFILE>)
{    
     my @pindetails = <INFILE>;
     <INFILE>;
     my @rest = <INFILE>;
     print "@rest";
}

Posted by Tim (Tim), 25 October 2007
Thanks foe the reply.
I tried but not working...
i have pasted the code.

use warnings;
print "Please enter the input file name\n";
 $file = <STDIN>;
 open(INFILE, $file);

foreach (<INFILE>)
{    
     my @pindetails = <INFILE>;
     <INFILE>;
     my @rest = <INFILE>;
     print "@rest";
}

Posted by admin (Graham Ellis), 25 October 2007
try replacing

$file = <STDIN>;  
open(INFILE, $file);

with

chop($file = <STDIN>);  
open(INFILE, $file) or die;

a) You need to remove the new line character off the end of the file name you type in.

b) You really ought to check that your open works properly.  The call to the die function will prevent you trying to process from a non-existant file handle.

There are some other things in you code which also need attention - but do get the file reading in first

Posted by KevinAD (KevinAD), 25 October 2007
use warnings;
print "Please enter the input file name: ";  
chomp($file = <STDIN>);
open(INFILE, $file) or die "$!";
<INFILE>;
print <INFILE>;
close INFILE;

Posted by george_Ball (george), 25 October 2007
foreach (<INFILE>)
{    
     my @pindetails = <INFILE>;

This line reads in the *entire* file and stores it one line per array element in @pindetails. That's probably why you're not seeing what you expect...

     <INFILE>;

This line is called in scalar context so we read *one* line, and discard it...

     my @rest = <INFILE>;

This line attempts to read *all* the remaining lines since we are calling it in array context but, like the line above, the <INFILE> operator will return undef because we are attempting to read beyond end of file.

     print "@rest";

My guess is that you'll not be seeing anything from this print statement, cos there will be nothing in the array..?

}


Posted by KevinAD (KevinAD), 25 October 2007
Quote:
My guess is that you'll not be seeing anything from this print statement, cos there will be nothing in the array..?


I concur.

Posted by george_Ball (george), 26 October 2007
Suspect what you really want to do here is...

open (<IN>, $file) or die $!;
my $pindetails = <IN>;
my @rest = <IN>;

foreach ( @rest ) {
 # Do something with each of the remaining lines
}

Notice that the $pindetails variable is a *scalar*. So it gets the first line; all the rest go into the @rest array... Now your iteration steps through the elements one at a time, effectively taking you through the lines of the file...

Posted by Tim (Tim), 26 October 2007
hello all,

Thanks for reply. But still im facing the same problem.

use warnings;

print "Please enter the input file name\n";
 chop($file = <STDIN>);
 open(INFILE, $file);

 
print "Enter output file name\n";
$file1 = <STDIN>;
chomp $file1;
open ( OUT, ">$file1");
 

 
 my $pindetail = <INFILE>;
 my @pdetails = <INFILE>;
 foreach (@pdetails)
 {    
           
       
     @pindetails = split(/,/, $pdetails);
           $ptype = $pindetails[1];
               $pname = $pindetails[0];
     $pnumber = $pindetails[2];
     @pnumber = split(/\n/, $pnumber);
                .......
 }





Posted by george_Ball (george), 26 October 2007
 
I suspect the "problem" is now different, there are issues with your loop now...

 ...  
 foreach (@pdetails)

Should be foreach $pindetails (@pindetails)

The way you have it, each line is going to be put into $_.
So the split will be looking at an undef and not give you anything...

@pindetails = split(/,/, $pdetails);
  $ptype = $pindetails[1];
     $pname = $pindetails[0];
$pnumber = $pindetails[2];

@pnumber = split(/\n/, $pnumber);

Not sure what you are trying to do here, but your value in $pnumber will possibly have a newline at the end (if itwas the last element on created by the split() call above), it certainly doesn't seem to make sense to split on the newline char.

      .......
 }





This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho