CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
taking alternate values from textfile

 



psr
Novice

Apr 22, 2013, 7:32 PM

Post #1 of 10 (1213 views)
taking alternate values from textfile Can't Post

Hi All,

I got stuck with taking alternate lines like 1,3,5...from a text file and print those lines with integers eg:1,2 and add them,print those integers from input text file in first line and there output in second line,print the third line integers from input text file and print output in fourth line...in to other text file.

I don't know how to select the alternate lines..and do calculations like a+b.Any ideas will be helpful

thanks in advance


Laurent_R
Veteran / Moderator

Apr 23, 2013, 1:47 AM

Post #2 of 10 (1208 views)
Re: [psr] taking alternate values from textfile [In reply to] Can't Post

Hi,

the $. special variable contains the number of the line last read in your file. You can just check if it is an odd number ($. is odd if ($. % 1) is true).

For the rest of your requirement, please provide a sample of your input file and show how you want the output to be.


psr
Novice

Apr 23, 2013, 12:22 PM

Post #3 of 10 (1195 views)
Re: [Laurent_R] taking alternate values from textfile [In reply to] Can't Post

Input file like
1 2
Add
2 3
Add


Output file like
1 2
3
2 3
5


It's just an example,thanks for replying Laurent.


Laurent_R
Veteran / Moderator

Apr 24, 2013, 5:24 AM

Post #4 of 10 (1184 views)
Re: [psr] taking alternate values from textfile [In reply to] Can't Post

HI,

this a minimal example:


Code
my ($d, $e); 
while (<$INPUT>) {
if ($. % 1) {
chomp;
($d, $e) = split;
} else {
print $d + $e, "\n";
}
}



FishMonger
Veteran / Moderator

Apr 24, 2013, 6:10 AM

Post #5 of 10 (1182 views)
Re: [psr] taking alternate values from textfile [In reply to] Can't Post

This is a fibonacci homework assignment.

What have you tried?

What errors/warnings are you receiving?


psr
Novice

Apr 24, 2013, 9:33 PM

Post #6 of 10 (1162 views)
Re: [FishMonger] taking alternate values from textfile [In reply to] Can't Post

Hi FishMonger,

Its not Fibonacci assignment,I am doing some testing.

Hi Laurent,

I am able to select the specific line using

awk 'NR%2==0' file1.txt .....which selects only second line

my file now looks like:for example

1 2 4

5 6 7

8 9 10

I need to code to do addition and my output file should look like

1 2 4

7

5 6 7

18

8 9 10

27


BillKSmith
Veteran

Apr 25, 2013, 5:33 AM

Post #7 of 10 (1156 views)
Re: [psr] taking alternate values from textfile [In reply to] Can't Post


Code
use strict; 
use warnings;
use v5.14.0;
while (<DATA>){
print $_, "\n", s/(\d+)\D+(\d+)\D+(\d+)/$1+$2+$3/er, "\n";
<DATA>; # Skip blank line
}
__DATA__
1 2 4

5 6 7

8 9 10


UPDATE: Added use version to code.
Good Luck,
Bill

(This post was edited by BillKSmith on Apr 29, 2013, 5:42 AM)


psr
Novice

Apr 28, 2013, 8:28 PM

Post #8 of 10 (1117 views)
Re: [BillKSmith] taking alternate values from textfile [In reply to] Can't Post

Hi Bill,

Its giving me syntax error,it need to read eg:file1.txt with the above specified input values and write output to file2.txt.


BillKSmith
Veteran

Apr 29, 2013, 5:31 AM

Post #9 of 10 (1107 views)
Re: [psr] taking alternate values from textfile [In reply to] Can't Post

The /r option was new in perl version 5.14.0. Are you using an older perl?

You are posting in the intermediate forum. You should know how to open files for input and output. Show us what you have so far.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 29, 2013, 8:49 AM

Post #10 of 10 (1102 views)
Re: [psr] taking alternate values from textfile [In reply to] Can't Post

This is really easy.

This is my foo.txt file:


Code
1 12 3 
34 2 1
16 2 1 67


Now I use this command ender the Unix prompt:


Code
perl -ne '$c=0; $c+=$_ foreach (split); print $_, "$c\n"' foo.txt

And this prints the following to the screen:

Code
1 12 3 
16
34 2 1
37
16 2 1 67
86


Then you can just redirect output to a file.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives