Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

looping issue

0 views
Skip to first unread message

dakin999

unread,
Jun 30, 2008, 3:45:29 AM6/30/08
to
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??

rthangam

unread,
Jun 30, 2008, 5:07:26 AM6/30/08
to

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.

Gunnar Hjalmarsson

unread,
Jun 30, 2008, 4:46:57 AM6/30/08
to

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

Tad J McClellan

unread,
Jun 30, 2008, 7:49:49 AM6/30/08
to
rthangam <ramesh.t...@gmail.com> wrote:

> 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/"

Jürgen Exner

unread,
Jun 30, 2008, 7:38:39 AM6/30/08
to
dakin999 <akhi...@gmail.com> wrote:
> 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
> --
> --
> --
> }
> }

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

Ben Morrow

unread,
Jun 30, 2008, 11:47:57 AM6/30/08
to

Quoth dakin999 <akhi...@gmail.com>:

> 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

--
I have two words that are going to make all your troubles go away.
"Miniature". "Golf".
[b...@morrow.me.uk]

xho...@gmail.com

unread,
Jun 30, 2008, 12:28:51 PM6/30/08
to

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.

Dr.Ruud

unread,
Jul 2, 2008, 3:37:54 PM7/2/08
to
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.

--
Affijn, Ruud

"Gewoon is een tijger."

0 new messages