Sponsored Content
Full Discussion: Perl while loop question
Top Forums Shell Programming and Scripting Perl while loop question Post 302340857 by KevinADC on Tuesday 4th of August 2009 01:06:21 PM
Old 08-04-2009
What will happen in your code is that you will get the first line of "hual" and then read through all the lines of "land" then when you get the second line of "hual" there will be no more lines from "land" to search through because the file pointer for that handle will be at the end of the file. Try this to see what I mean:

Code:
#!/usr/bin/perl
use strict;
use warnings;
my $haul_file = "mack_haul.txt";
my $lands_files = "mack_land.txt";
open ("land",$lands_files) || die "cant open\n";
open ("haul",$haul_file) || die "Cant Open\n";

while(my $haulline=<haul>){
   print "[hual] $_";
   while(my $landslines=<land>){
      print "[land] $_";
   }
}
close("haul") || die "Cant close1";
close("land") || die "cant close2";


Side note: use all uppercase characters for filehandles or better use lexical filehandles. lower-case words should be avoided in perl since all of perls built-in functions are all lower-case.

If you need to start the search in "land" from the beginning of the file for each line of "haul" you can use seek() to return the file pointer back to the beginning of the "land" file. See seek() for details.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop question

hi, how would i go about making a loop which gets each line from a single text file, set it to a variable and then print it to screen? thanks eg: #!/bin/sh FILE="somefile.txt" text_line="" what kind of loop would use here? (18 Replies)
Discussion started by: strike
18 Replies

2. Shell Programming and Scripting

while loop question

while do print What is the next device number to be added to $dgroup? print Press \<Enter\> if there are no more devices to be added. read dev_num export dev_num symld -g $dgroup -sid $sname add dev $dev_num done In this while... (2 Replies)
Discussion started by: stepnkev
2 Replies

3. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

4. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

loop question

hey guys what im trying to do is do a simple script that will ask for a password and on the 5th time it says access denied if the right password is still not entered this is what i have so far can anyone help me im not good with scripting thanks in advance #!/bin/bash secretname=secret... (2 Replies)
Discussion started by: randallrivy11
2 Replies

6. Shell Programming and Scripting

For Loop Question

I am struggling with the for loop. I have a file name heros.txt and I would like to go through a list in file where.txt and see if I can find the name from where inside heros. One of the problems that I am having is I dont understand how to setup the for loop to find the list to search.:wall: ... (6 Replies)
Discussion started by: captaindoogles
6 Replies

7. Shell Programming and Scripting

For loop question

Hi, I'm trying to put together a small script that will read a txt file that contains a list of two columns. Each column is the name of a folder.. e.g. AIX Server1 AIX Server2 AIX Server3 $ for i in `cat /opt/apacheprod/scripts/input/copy_list.txt` do PLATFORMVAR=`awk ' { print $1 } '... (7 Replies)
Discussion started by: Jazmania
7 Replies

8. Shell Programming and Scripting

For loop question

I have two files. In file one, there are many columns, but only two of interest to me. Column 1 contains a list of individuals, defined by an ID number. Column 10 contains the diagnosis that each individual has (I am a physician). All together, there are 3000 lines in this file, one line per... (2 Replies)
Discussion started by: awc228
2 Replies

9. UNIX for Dummies Questions & Answers

Question about for loop

Hi, I have code like below disk_list=$(ls /root/file1) for disk in $disk_list do pvcreate $i done I know what pvcreate command does, but I do not understand what this $i do here. can someone please explain. (2 Replies)
Discussion started by: stew
2 Replies

10. Shell Programming and Scripting

Question around For Loop

Hi, I have a variable called result that get the the below value i.e two lines assigned../logs/mymac/myserver.log:####<Jun 7, 2015 12:56:54 PM EDT> <myserver.my.bank.com> <mymac> < ExecuteThread: '5' for queue:\ 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <1434640> <BEA-0900>... (0 Replies)
Discussion started by: shifahim
0 Replies
apache_mod_perl-108~358::mod_perl-2.0.7::docs::api::APR:UserlContributed Perl Doapache_mod_perl-108~358::mod_perl-2.0.7::docs::api::APR::PerlIO(3)

NAME
APR::PerlIO -- Perl IO layer for APR Synopsis # under mod_perl use APR::PerlIO (); sub handler { my $r = shift; die "This Perl build doesn't support PerlIO layers" unless APR::PerlIO::PERLIO_LAYERS_ARE_ENABLED; open my $fh, ">:APR", $filename, $r->pool or die $!; # work with $fh as normal $fh close $fh; return Apache2::Const::OK; } # outside mod_perl % perl -MAPR -MAPR::PerlIO -MAPR::Pool -le 'open my $fh, ">:APR", "/tmp/apr", APR::Pool->new or die "$!"; print $fh "whoah!"; close $fh;' Description "APR::PerlIO" implements a Perl IO layer using APR's file manipulation API internally. Why do you want to use this? Normally you shouldn't, probably it won't be faster than Perl's default layer. It's only useful when you need to manipulate a filehandle opened at the APR side, while using Perl. Normally you won't call open() with APR layer attribute, but some mod_perl functions will return a filehandle which is internally hooked to APR. But you can use APR Perl IO directly if you want. Prerequisites Not every Perl will have full "APR::PerlIO" functionality available. Before using the Perl IO APR layer one has to check whether it's supported by the used APR/Perl build. Perl 5.8.x or higher with perlio enabled is required. You can check whether your Perl fits the bill by running: % perl -V:useperlio useperlio='define'; It should say define. If you need to do the checking in the code, there is a special constant provided by "APR::PerlIO", which can be used as follows: use APR::PerlIO (); die "This Perl build doesn't support PerlIO layers" unless APR::PerlIO::PERLIO_LAYERS_ARE_ENABLED; Notice that loading "APR::PerlIO" won't fail when Perl IO layers aren't available since "APR::PerlIO" provides functionality for Perl builds not supporting Perl IO layers. Constants "APR::PerlIO::PERLIO_LAYERS_ARE_ENABLED" See Prerequisites. API
Most of the API is as in normal perl IO with a few nuances listed in the following sections. META: need to rework the exception mechanism here. Current success in using errno ($!) being set (e.g. on open()) is purely accidental and not guaranteed across all platforms and functions. So don't rely on $!. Will use "APR::Error" for that purpose. "open" Open a file via APR Perl IO layer. open my $fh, ">:APR", $filename, $r->pool or die $!; arg1: $fh ( GLOB filehandle ) The filehandle. arg2: $mode ( string ) The mode to open the file, constructed from two sections separated by the ":" character: the first section is the mode to open the file under (>, <, etc) and the second section must be a string APR. For more information refer to the open entry in the perlfunc manpage. arg3: $filename ( string ) The path to the filename to open arg4: $p ( "APR::Pool" ) The pool object to use to allocate APR::PerlIO layer. ret: ( integer ) success or failure value (boolean). since: 2.0.00 "seek" Sets $fh's position, just like the "seek()" Perl call: seek($fh, $offset, $whence); If $offset is zero, "seek()" works normally. However if $offset is non-zero and Perl has been compiled with with large files support ("-Duselargefiles"), whereas APR wasn't, this function will croak. This is because largefile size "Off_t" simply cannot fit into a non-largefile size "apr_off_t". To solve the problem, rebuild Perl with "-Uuselargefiles". Currently there is no way to force APR to build with large files support. since: 2.0.00 C API
The C API provides functions to convert between Perl IO and APR Perl IO filehandles. META: document these See Also mod_perl 2.0 documentation. The perliol(1), perlapio(1) and perl(1) manpages. Copyright mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. Authors The mod_perl development team and numerous contributors. perl v5.16.2 2011-02-07 apache_mod_perl-108~358::mod_perl-2.0.7::docs::api::APR::PerlIO(3)
All times are GMT -4. The time now is 10:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy