Question: Extracting Set Of Numbers From A File
0
gravatar for Cynthia
8.2 years ago by
Cynthia190
Cynthia190 wrote:

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 • 2.1k views
ADD COMMENTlink written 8.2 years ago by Cynthia190
4

What did you wrote so far ?

ADD REPLYlink written 8.2 years ago by Pierre Lindenbaum118k
1

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

ADD REPLYlink written 8.2 years ago by Thaman3.2k

@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 REPLYlink written 8.2 years ago by Cynthia190

@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 REPLYlink written 8.2 years ago by Cynthia190

@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 REPLYlink written 8.2 years ago by Cynthia190
3
gravatar for Zhidkov
8.2 years ago by
Zhidkov560
Israel
Zhidkov560 wrote:

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 COMMENTlink modified 8.2 years ago by Michael Dondrup45k • written 8.2 years ago by Zhidkov560

thank you Michael

zhidkov

ADD REPLYlink written 8.2 years ago by Zhidkov560

thankyou very much i ve got it

ADD REPLYlink written 8.2 years ago by Cynthia190
1
gravatar for Farhat
8.2 years ago by
Farhat2.9k
Pune, India
Farhat2.9k wrote:

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 COMMENTlink written 8.2 years ago by Farhat2.9k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1220 users visited in the last hour