Extracting Set Of Numbers From A File
2
0
Entering edit mode
13.3 years ago
Cynthia ▴ 190

In a text file there are numbers. for eg:

human 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

chimpangee 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 63 64 65 66 67 68 69 72 74 75 78 79 81 82 83 86 87 88 89 92 94 95 98 99 100 101 106 108 110 113 115 116 117 118 119 120

each number is separated with a single space. so, i ve to write a perl program which recognises each number and multiply each number with 3.

and the multiplied output must come in a another file which can be of excel(xls) or text format(txt)

perl • 3.8k views
ADD COMMENT
4
Entering edit mode

What did you wrote so far ?

ADD REPLY
1
Entering edit mode

Give some effort in writing some lines of codes, instead of expecting someone to write for you.

ADD REPLY
0
Entering edit mode

@Thaman i ve given my effort andi could get it thats y i asked. and i m new to this programming world with very less time in hand. thats y posted the query in this forum.

i misunderstood this forum and expected it to be helpful. sorry to all

ADD REPLY
0
Entering edit mode

@Thaman i ve given my effort but i could get it thats y i asked. and i m new to this programming world with very less time in hand. thats y posted these queries in this forum. i misunderstood this forum and expected it to be helpful in writing programs. sorry to all

ADD REPLY
0
Entering edit mode

@Thaman i ve given my effort but i couldn't get it thats y i asked. and i m new to this programming world with very less time in hand. thats y posted these queries in this forum. i misunderstood this forum and expected it to be helpful in writing programs. sorry to all – Payaliya yesterday

ADD REPLY
3
Entering edit mode
13.3 years ago
Zhidkov ▴ 600
use strict;
use warnings;

my $in_file = shift @ARGV;  #in command line type the name of your file

open (IN, $in_file) or die $!; # open INPUT file
open(OUT, ">$in_file.multiplied.csv") or die $!; # open OUTPUT file to write in
while (my $line = <IN>){    #read file line by line

    chomp $line; #remove the end of line
    my @seq = split(/\s/, $line); # split line by space and populate an array
    my $name = shift @seq;  # remove first element of array (sample name)
    my @new_seq = map {($_)*3} @seq;     # multiple each element in array by 3 and populate new array
    # print write to OUTPUT file results in csv        
    #  format (can be opened with xcl):
    print OUT join ("\,", $name,@new_seq)."\n";
}

close IN;   #close INPUT
close OUT or die $!;    #close OUTPUT

Don't know why it happened but in while command should be: $line= <> IN between the <>

Ilia

ADD COMMENT
0
Entering edit mode

thank you Michael

zhidkov

ADD REPLY
0
Entering edit mode

thankyou very much i ve got it

ADD REPLY
1
Entering edit mode
13.3 years ago
Farhat ★ 2.9k

If you don't mind moving beyond perl the following code will do the job using awk.

gawk '{printf "%s, ",$1; for (i=2; i<=NF; i++) printf "%d,",$i*3; printf "\n";}' infile > outfile
ADD COMMENT

Login before adding your answer.

Traffic: 1945 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6