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: Intermediate:
Not going thro' the loop

 



Divya
New User

May 1, 2013, 11:26 AM

Post #1 of 7 (341 views)
Not going thro' the loop Can't Post

Hello,

Please let me know what the problem is with this code, I am trying to print the first line and a part of second line from a bunch of files fed onto an array. It just prints the output from the first file and does not go thro' the loop. I have tried different variations but am not able to figure out the mistake.
Thanks in advance.

#!/usr/bin/perl
open (IN, "scores.txt");
open (OUT, ">>outfile.txt");
while (<IN>){
@lines=$_;
foreach $lines (@lines){
open (FILE, $lines);
while (<FILE>){
$ID = substr ($_,10);
print OUT if $. == 1;
print OUT $ID if $. == 2;
}
}
}


g4143
Novice

May 1, 2013, 12:43 PM

Post #2 of 7 (335 views)
Re: [Divya] Not going thro' the loop [In reply to] Can't Post

Don't really know what your trying to accomplish here. Could you elaborate?


Divya
New User

May 1, 2013, 12:46 PM

Post #3 of 7 (332 views)
Re: [g4143] Not going thro' the loop [In reply to] Can't Post

 I am trying to print the first line and a part of second line from a bunch of files fed onto an array (@lines from scores.txt, which contains a list of filenames). I want to open each of the files present in scores.txt and then print the first line and the substr part of second line from each.

Thanks


g4143
Novice

May 1, 2013, 12:54 PM

Post #4 of 7 (331 views)
Re: [Divya] Not going thro' the loop [In reply to] Can't Post

OK, I see what your trying to do here.


g4143
Novice

May 1, 2013, 1:00 PM

Post #5 of 7 (330 views)
Re: [Divya] Not going thro' the loop [In reply to] Can't Post

Try starting with this

Code
#!/usr/bin/perl 

use warnings;
use strict;
use autodie qw/open close/;

print "Enter input filename->";
chomp(my $ifilename = <STDIN>);

my @arr_filenames = ();

open(my $IFILE, "<", $ifilename);

while ( <$IFILE> )
{
chomp;
if ( -e -r $_ )#Check to see if filename exist and is readable
{
push @arr_filenames, $_;#push filenames $_ onto array
}
}

close($IFILE);

#do something with array of filenames - @arr_filenames

__END__



Laurent_R
Enthusiast / Moderator

May 2, 2013, 12:17 AM

Post #6 of 7 (307 views)
Re: [Divya] Not going thro' the loop [In reply to] Can't Post

This part is wrong:


Code
while (<IN>){ 
@lines=$_;
foreach $lines (@lines){


Either you iterate over the file:


Code
while (my $line = <IN>){ 
{ # do some thing with each individual line


or you slurp the file into an array:


Code
@lines =  <IN>;  
foreach $lines (@lines) {
# do something will all lines in the array



Kenosis
User

May 4, 2013, 2:26 PM

Post #7 of 7 (288 views)
Re: [Divya] Not going thro' the loop [In reply to] Can't Post

Perhaps the following will be helpful:

Code
use strict; 
use warnings;

FILE: for my $file (<>) {
push @ARGV, $file;
while (<>) {
if ( $. == 1 ) {
print;
}
elsif ( $. == 2 ) {
print substr $_, 10;
close ARGV;
next FILE;
}
}
}


Usage: perl script.pl scores.txt >outfile.txt

The script iterates through the names in scores.txt, pushing each onto Perl's @ARGV to let Perl handle the file i/o.

As a file's being read, if it's the first line, that line is printed. If the second, a substring of that line is printed, the current file is closed and the next file is processed.

The >outfile.txt notation directs output to a file. Omit that last parameter if you'd first like to see the results on the screen.


(This post was edited by Kenosis on May 4, 2013, 5:08 PM)

 
 


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

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