ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Scratch that, I got it working... sort of. I need to now know how to chomp every OTHER line. Here is my current code:
Code:
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $file = $ARGV[0]);
open (FILE, "$file") or die "Error: $!";
while(<FILE>){
print "Chomping " . $_ . "\n";
chomp $_;
}
close FILE;
edit: Now it's not working WTH!? What does chomp remove? CR? LF? CR/LF?
Would this have anything to do with the fact that it's a CSV file?
Scratch that, I got it working... sort of. I need to now know how to chomp every OTHER line. Here is my current code:
Code:
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $file = $ARGV[0]);
open (FILE, "$file") or die "Error: $!";
while(<FILE>){
print "Chomping " . $_ . "\n";
chomp $_;
}
close FILE;
Now it's not working WTH!? What does chomp remove? CR? LF? CR/LF?
Chomp removes any trailing input record separator. Could be a CR, CR/LF, etc., which can be defined to be whatever you want.
Read more here: http://perldoc.perl.org/functions/chomp.html
The $. variable holds the current line number. So
Code:
while (<FILE>) {
<do something here> $_ if 1 & $. ;
}
may do the trick. After you've opened the input file, use the $. variable to tell you how many lines have been read. This will print odd-numbered lines. Replace "if" with "unless" to print even lines. There's also:
Code:
while(<>)
if($i++%2){ print; }
Also, every time you use the input operator (<>), you read a new line. You can read it into a variable, but you're not doing that, so it goes directly into the special variable $_. You read a line in the while condition, and then clobber it by reading the next line from the file. I'd suggest against using the $_ to work with, but assigning the line(s) to variables, and splitting them out. The $_ is ok for very small things, but not much else. Too many chances to clobber something.
Thanks for the reply, I just realized my problem was that I wasn't outputting to anything (other than STDOUT). For some reason I thought it would overwrite the file with my changes :P. Well anyways, I have it outputting to a file now but something is totally borked with my method lol. This obviously doesn't work:
Code:
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $file = $ARGV[0]);
open (FILE, "$file") or die "Error: $!";
while(<FILE>){
print "Chomping " . $_ . "\n" if 1 & $.;
chomp($_) if 1 & $.;
open NEWFILE, ">Desktop/abc.csv";
print NEWFILE $_;
}
print "Output Done!\n";
I can't figure out how to accomplish this. It's messed because since I'm opening the output file, open closes the previous one automatically, which my while loop is running on. Once I figure this out I can implement the changes you mentioned, I just need to figure out how to have 1 file open and reading, while writing to another at the same time....
Thanks for the reply, I just realized my problem was that I wasn't outputting to anything (other than STDOUT). For some reason I thought it would overwrite the file with my changes :P. Well anyways, I have it outputting to a file now but something is totally borked with my method lol. This obviously doesn't work:
Code:
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $file = $ARGV[0]);
open (FILE, "$file") or die "Error: $!";
while(<FILE>){
print "Chomping " . $_ . "\n" if 1 & $.;
chomp($_) if 1 & $.;
open NEWFILE, ">Desktop/abc.csv";
print NEWFILE $_;
}
print "Output Done!\n";
I can't figure out how to accomplish this. It's messed because since I'm opening the output file, open closes the previous one automatically, which my while loop is running on. Once I figure this out I can implement the changes you mentioned, I just need to figure out how to have 1 file open and reading, while writing to another at the same time....
Open your files at the top, before the loop. Like this:
Code:
#!/usr/bin/perl
use strict;
use warnings;
open (FILE, "$file") or die "Error: $!";
open NEWFILE, ">Desktop/abc.csv";
chomp(my $file = $ARGV[0]);
while(<FILE>){
print "Chomping " . $_ . "\n" if 1 & $.;
chomp($_) if 1 & $.;
print NEWFILE $_;
}
print "Output Done!\n";
Thanks a lot for the help, I almost have it sorted out. This is now my current code:
Code:
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $file = $ARGV[0]);
open (FILE, "$file") or die "Error: $!";
open NEWFILE, ">Desktop/abc.csv" or die "Error: $!";
while(<FILE>){
chomp($_) if 1 & $.;
print NEWFILE $_;
}
print "\nOutput Done!\n";
It works, except doesn't seem to be chomping anything. It's just duplicating the existing file... instead of chomping every 2nd line (starting at line 1)
Thanks a lot for the help, I almost have it sorted out. This is now my current code:
Code:
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $file = $ARGV[0]);
open (FILE, "$file") or die "Error: $!";
open NEWFILE, ">Desktop/abc.csv" or die "Error: $!";
while(<FILE>){
chomp($_) if 1 & $.;
print NEWFILE $_;
}
print "\nOutput Done!\n";
It works, except doesn't seem to be chomping anything. It's just duplicating the existing file... instead of chomping every 2nd line (starting at line 1)
And what is
Code:
if 1 & $.
supposed to mean ? Someone wants to show knowledge in the way numbers are represented internally ? If yes, does this task require this knowledge ? Does this knowledge help ? Dose such writing make code more readable ?
Also, why does one think $. variable is appropriate here ? I strongly suggest to read the part of
The $. variable holds the current line number. So
Code:
while (<FILE>) {
<do something here> $_ if 1 & $. ;
}
So I want to chomp line 1, so line 1 and 2 join. next, there will be a new line 2 because 1 and the old line 2 should be joined... right? Perhaps I'm being confusing.. I jsut want to turn this:
So I want to chomp line 1, so line 1 and 2 join. next, there will be a new line 2 because 1 and the old line 2 should be joined... right? Perhaps I'm being confusing.. I jsut want to turn this:
1
2
3
4
into this:
12
34
Who "you" ? Have you read
perldoc perlvar
?
The document uses different wording and has a number of suggestions/alternatives.
So I want to chomp line 1, so line 1 and 2 join. next, there will be a new line 2 because 1 and the old line 2 should be joined... right? Perhaps I'm being confusing.. I jsut want to turn this:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.