LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2007, 07:57 AM   #1
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
help with perl


Hi All,

In the following piece of script, am retrieving the corresponding value based on the line no from another file.

Is there any way to reiterate the implicit file pointer in perl just like fseek without having to close and open the file each time.

Many thanks for your help!

Code:
#! /opt/third-party/bin/perl
                                                                                                                                                             
my $badFile    = "bad.txt";
my $goodFile   = "good.txt";
my $outputFile = "out.txt";
                                                                                                                                                             
my $globalLineNo = 0;
my ($badNum, $part, $line, $result);
                                                                                                                                                             
open(BAD, $badFile);
open(GOOD, $goodFile);
open(OUT, "> $outputFile");
                                                                                                                                                             
while ( chomp($badLine = <BAD>) )
{
  my @split_fields = split(/,/, $badLine);
  $badNum = @split_fields[0];
  $part   = @split_fields[1];
                                                                                                                                                             
  my @split_fields = split(/-/, $part);
  $line = @split_fields[1];
                                                                                                                                                             
  $result =  subFunction($line);
  print "$badNum,$result\n";
  print OUT "$badNum,$result\n";
}
                                                                                                                                                             
close(BAD);
close(GOOD);
close(OUT);
                                                                                                                                                             
exit(0);
                                                                                                                                                             
sub subFunction
{
  my $lno = shift;
                                                                                                                                                             
  if ( $lno < $globalLineNo ) {
    close(GOOD);
    open(GOOD, $goodFile);
    $globalLineNo = 0;
  }
                                                                                                                                                             
  while ( chomp($goodLine = <GOOD>) )
  {
    $globalLineNo++;
    if ( $globalLineNo == $lno ) {
      return $goodLine;
    }
  }
  return "Not Found";
}
 
Old 01-12-2007, 09:04 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
seek <handle>, <offset>, <whence>
where whence is
0=beginning
1=current pos
2=eof

Last edited by jim mcnamara; 01-12-2007 at 09:05 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM
can i set ~/public_html/perl as a perl directory for apache? doublefailure Linux - Networking 1 07-09-2002 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration