Re: looping issue



On Jun 30, 12:45 pm, dakin999 <akhils...@xxxxxxxxx> wrote:
Hi, I have following code:

foreach my $row (@$array_ref) {
my ( $usr, $usr_det, $pwd_val) = @$row;
#print "\tuser id :$usr\n";
#print "\tcard no :$usr_det\n";
#print "\tpasswd :$pwd_val\n";
open (FILE, "<file_name") || die "Could not open the file: $!";
while (<FILE>) {
chomp;
(my $nusr_id) = split(); #read into a variable
--
--
--
}
}

The problem is in the looping of input <FILE> that I need to read for
each $row. Basically there can be more than 1 $row values and I need
to pick a new line from <FILE> for each $row entry.

Any suggestions for doing this??

You can do it this way. Open the file before the for ... loop and use
readline() function which accepts the filehandle as parameter to get a
line at a time. For eg.,

open(FH,"test.txt") or die $!;
my $line = readline(FH);
print $line;

$line = readline(FH);
print $line;

You can also try some other modules like FileHandle which is object
oriented and also has methods like getline(), getlines() etc.
.



Relevant Pages

  • Re: MAC Address Comparison
    ... I am wondering why do I get this "Use of uninitialized value" warning ... You need to chomp $input to remove the line ending. ... You should not use DATA as your filehandle, it's the name of one of ...
    (comp.lang.perl.misc)
  • Re: fork messing up parent filehandle
    ... altough the children do not use this filehandle. ... looping through the file, the filehandle is magically reset to the ... initial child spawning done... ...
    (comp.lang.perl.misc)
  • FileHandle::Unget 0.13 released
    ... FileHandle::Unget is a drop-in replacement for FileHandle which allows more ... Added detection for a case in which a Perl bug will be exercised on Windows. ... Fixed a bug in getline and getlines which would cause the routines to not ...
    (comp.lang.perl.modules)
  • Re: MAC Address Comparison
    ... You need to chomp $input to remove the line ending. ... You should not use DATA as your filehandle, it's the name of one of ...
    (comp.lang.perl.misc)
  • Re: MAC Address Comparison
    ... You need to chomp $input to remove the line ending. ... You should not use DATA as your filehandle, it's the name of one of ...
    (comp.lang.perl.misc)