LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-04-2012, 02:04 PM   #1
jfsiegel
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Rep: Reputation: Disabled
Perl problems


Hello,
I had posted a short time ago about a problem I was having trying to sort through a file via awk over in the Solaris forum but while the solution worked I am trying to see if I can handle the same issue as a Perl program. What I am trying to do is to get a repeatable program that can target the 10th and 11th column of data and extract it from one file and place it into a new file. There will not always be an 11th column. I would like to get the results from each line of the input file concatonated onto the end of the previous line ignoring any newlines. The ability to designate files for input and output is relevant because I want to be able to reuse the same program several times in a row on several different files.



Code:
#!/usr/bin/perl -w
print "Input File Name: ";
$input = <>;
print "Output File Name: ";
$parse = <>;

open ($input, "$input");
open ($parse, "$parse");
while (<$input>) {
    split (/ /);
    print $parse "$column[10],column[11]\n";
    }
close ($input);
close ($parse);
What I have been getting when I try to run this is an error for each line of the input file that reads as follows:

Code:
use of uninitialized value in concatenation (.) or string at ./script.pl line 11, <input_file.txt> line 1.
use of uninitialized value in concatenation (.) or string at ./script.pl line 11, <input_file.txt> line 1.
Filehandle output.pl opened only for input at ./script.pl line 11, <input_file.txt> line 1.
I am really trying to figure this out in Perl terms and I am learning on the fly. Thanks for your assistance in advance.
 
Old 05-04-2012, 05:16 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi, I think you mean this:
Code:
#!/usr/bin/perl 

use strict ;
use warnings ;

print "Input File Name: ";
my $input = <>;
print "Output File Name: ";
my $parse = <>;

open (INPUT, $input);
open (PARSE, ">$parse"); # don't forget to open for writing
my @column ;  # don't forget to initialize @column
while (<INPUT>) {
     @column = split (/ /);
     print PARSE "$column[1],$column[2]\n"; # I didn't have a file with that much columns ;)
}
close (INPUT);
close (PARSE);
Markus

Last edited by markush; 05-04-2012 at 05:18 PM. Reason: added a comment
 
1 members found this post helpful.
Old 05-04-2012, 05:44 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by markush View Post
Hi, I think you mean this:
Code:
#!/usr/bin/perl 

use strict ;
use warnings ;

print "Input File Name: ";
my $input = <>;
print "Output File Name: ";
my $parse = <>;

open (INPUT, $input);
open (PARSE, ">$parse"); # don't forget to open for writing
my @column ;  # don't forget to initialize @column
while (<INPUT>) {
     @column = split (/ /);
     print PARSE "$column[1],$column[2]\n"; # I didn't have a file with that much columns ;)
}
close (INPUT);
close (PARSE);
Markus
And "INPUT" and "PARSE" are global - for now good reason.

You can write

Code:
open(my $ifh, $input) or die "cannot open '$input' file for reading: $!";
.
 
Old 05-07-2012, 09:00 AM   #4
jfsiegel
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks that almost got it. I had to add a Chomp statement to get the output I was looking for:
Code:
while (<INPUT>) }
     chomp;
     @column = split (/ /);
     print PARSE "$column[11]$column[12]";
}
Also I noted one thing...I received a great number of errors that read:
Code:
Use of unititialized value in concatenation (.) or string at ./Script line 17, <INPUT> line #####.
Mind you when I look at the output it is all there. What I think is happening is that there are only certain lines where I have a Column 12...but i needed to capture them nevertheless. I am wondering if there is a way to tell the program to print any colums at or past a designated number so that it can find them automatically without a screen full of what appear to be errors. Thanks!
 
Old 05-07-2012, 10:22 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jfsiegel View Post
Thanks that almost got it. I had to add a Chomp statement to get the output I was looking for:
Code:
while (<INPUT>) }
     chomp;
     @column = split (/ /);
     print PARSE "$column[11]$column[12]";
}
Also I noted one thing...I received a great number of errors that read:
Code:
Use of unititialized value in concatenation (.) or string at ./Script line 17, <INPUT> line #####.
Mind you when I look at the output it is all there. What I think is happening is that there are only certain lines where I have a Column 12...but i needed to capture them nevertheless. I am wondering if there is a way to tell the program to print any colums at or past a designated number so that it can find them automatically without a screen full of what appear to be errors. Thanks!
You have those warnings because your code tries to access array elements which do bot exist.

Rather than asking here first learn. For example, read about all built-in Perl functions: http://perldoc.perl.org/index-functions.html - look for 'exists', 'defined'.

Look for 'scalar' in http://perldoc.perl.org/perldata.html .
 
  


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
serious perl problems vmlinuz101 Linux From Scratch 1 11-27-2006 09:29 AM
problems with perl.. amachine7 Mandriva 2 01-02-2005 03:25 PM
Perl problems rickenbacherus Linux - Software 1 06-08-2004 11:57 PM
Perl problems Vindane Linux - Software 1 06-05-2004 05:09 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

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

All times are GMT -5. The time now is 11:28 AM.

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