looping issue

D

dakin999

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??
 
G

Gunnar Hjalmarsson

dakin999 said:
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??

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.
 
R

rthangam

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.
 
J

Jürgen Exner

dakin999 said:
foreach my $row (@$array_ref) { [...]
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.

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
--
 
B

Ben Morrow

Quoth dakin999 said:
Hi, I have following code:

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
 
X

xhoster

dakin999 said:
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??

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.
 
D

Dr.Ruud

Ben Morrow schreef:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,125
Messages
2,570,749
Members
47,302
Latest member
MitziWragg

Latest Threads

Top