Click here to get back home

looping issue

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
looping issue dakin999 06-30-2008
|--> Re: looping issue Gunnar Hjalmars...06-30-2008
| `--> Re: looping issue Tad J McClellan06-30-2008
Posted by dakin999 on June 30, 2008, 3:45 am
Please log in for more thread options
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??

Posted by Gunnar Hjalmarsson on June 30, 2008, 4:46 am
Please log in for more thread options
dakin999 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??

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

Posted by rthangam on June 30, 2008, 5:07 am
Please log in for more thread options
> 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.

Posted by Tad J McClellan on June 30, 2008, 7:49 am
Please log in for more thread options

> my $line = readline(FH);


Or use the more common equivalent:

my $line = <FH>;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Posted by Jürgen Exner on June 30, 2008, 7:38 am
Please log in for more thread options
> 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

Similar ThreadsPosted
looping through array July 11, 2004, 3:34 pm
Can You Figure Out This - Looping January 5, 2005, 6:42 pm
Looping through a log file March 4, 2005, 10:23 am
Looping (continued) April 23, 2005, 11:29 am
Algorithm with HoA looping November 11, 2006, 6:27 pm
looping questions September 28, 2007, 4:07 pm
looping through IP address range July 7, 2004, 1:11 am
Continous Looping of a List September 24, 2004, 3:08 pm
Looping Dir entires with space January 27, 2005, 9:05 pm
Looping almost the same repetitive lines April 22, 2005, 11:05 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap