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.
Please clarify what "this" means.
Possibly you mean that you want to look up a value in "file_name" for
each $row. That would indicate that you ought to store the file data in
a hash variable.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
> my $line = readline(FH);
Or use the more common equivalent:
my $line = <FH>;
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
This code reads and loops through all of <FILE> for each element of
@$array_ref.
> 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.
Are you saying that is not what you want but instead you want to loop
through @$array_ref and <FILE> in sync, i.e. for each element of
@$array_ref read exactly one line of <FILE>?
If so, then just do it:
open (FILE, "<file_name") || die "Could not open the file: $!";
foreach my $row (@$array_ref) {
[...]
$_ = <FILE>;
chomp;
(my $nusr_id) = split(); #read into a variable
--
--
--
}
jue
use warnings;
use strict;
open my $FILE, '<', 'file_name'
or die "can't open 'file_name': $!";
> 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";
my $line = <$FILE> or last;
chomp $line;
my ($nusr_id) = split;
#...
}
Ben
--
I have two words that are going to make all your troubles go away.
"Miniature". "Golf".
[b...@morrow.me.uk]
It looks like you are trying to join the array and the file. But you have
hidden what the join condition is behind the "--", which greatly limits
the specificity of the advice we can give. Most likely, something should
be put into a hash. Whether that is the contents of @$array_ref or
the contents of file_name, I can't tell based on what you have given us.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
> my $line = <$FILE> or last;
> chomp $line;
> my ($nusr_id) = split;
That split works on $_.
If you meant C<split "", $line;>, the preceding chomp is unnecessary.
--
Affijn, Ruud
"Gewoon is een tijger."