Thread: from perl to C

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    from perl to C

    Dear all,
    I am a newbie to C. I want to learn C very badly. i know the theory part of C, but i m not so good when it comes to writting real time programs in C. I m good at perl, but when i use the below program for a small file, it works fine. Not the same for a 10 gig file. So, i felt that writing the program in C would help. Please help me solve the problem, n also to learn C. Here is my problem.

    I am comparing 2 files, i take information of left and right values from file 2, and extract numbers at the beginning(left value) and end(right value) of every string-numbers (based on their unique ID which starts with '>') from file 1.
    Code:
    file1:
    
    >AAAT3R length=110
    40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 
    40 40 40 40 40 40 40 40 40 40 40 40 40 40 38 38 38 38 40 40 39 40 
    40 40 40 40 40 40 40 40 40 38 38 37 39 36 36 40 36 35 35 35 38 40 
    35 35 33 35 35 35 40 40 40 40 37 37 38 38 38 40 40 40 40 40 40 40 
    40 40 40 40 40 40 40 40 40 37 36 36 31 22 22 22 20 20 20 20 20 14
    >AAA2OJ length=70
    18 18 18 21 35 35 35 32 32 32 33 35 38 39 37 37 39 39 39 39 39 40 
    40 39 39 39 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 
    40 39 39 37 35 35 39 37 37 37 37 37 37 37 37 37 37 33 32 32 30 20 
    17 17 17 0
    
    file2:
    
    >AAAT3R_left length=6
    TACATA
    >AAAT3R_right length=62 ACTACTGATTTGATTATCTTTGATCTCTGTCGAACTAACTATATCTTAGTATGATCTTTAAT
    >AAA2OJ_left length=14
    TTTTGGACTATCTG
    >AAA2OJ_right length=14
    AGGCTGTTCTTTTN
    
    result file(expected)
    
    >AAAT3R_left length=6
    40 40 40 40 40 40
    >AAAT3R_right length=62
    40 40 40 38 38 37 39 36 36 40 36 35 35 35 38 40 35 35 33 35 35 35 40 40
     40 40 37 37 38 38 38 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 37
     36 36 31 22 22 22 20 20 20 20 20 14 >AAA2OJ_left length=14
    18 18 18 21 35 35 35 32 32 32 33 35 38 39
    >AAA2OJ_right length=14
    37 37 37 37 37 33 32 32 30 20 17 17 17 0
    [download]
    
    This is the code i have written so far, to get the desired output.
    
    #!/usr/bin/perl -w
    use strict;
    
    our ($File1, $File2) = qw/file1 file2/;
    open File1 or die "$File1: $!\n";
    open File2 or die "$File2: $!\n";
    
    my ($key, %results);
    while (<File1>){
        next if /^\s*$/;
        chomp;
        if (/^>\s*(\S+)/){
            $key = $1;
        }
        else {
            $results{$key} = [ split ];
        }
    }
    close File1;
    
    my ($len, $side, $str);
    while (<File2>){
        next if /^\s*$/;
        if (/^>([^_]+)_(left|right).*?(\d+)\s*$/){
            print;
            $str = $1;
            $side = $2;
            $len = $3;
        }
        else {
            my @list;
            @list = @{$results{$str}};
            if ($side eq 'left'){
                die "$str is too short for a left slice of $len!\n"
                unless @list >= $len;
                print "@list[0..$len-1]\n";
            }
            else {
                die "$str is too short for a right slice of $len!\n"
                unless @list >= $len;
                print "@list[@list-$len..$#list]\n";
            }
        }
    }
    close File2;
    Please help me do this in C.
    Last edited by Salem; 01-04-2009 at 11:38 PM. Reason: Fold long lines, indented perl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C structure in perl typemap
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 07-16-2003, 11:13 AM
  2. de facto perl book
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 05-22-2003, 04:43 AM
  3. perl program question
    By newbie2c in forum Tech Board
    Replies: 2
    Last Post: 02-03-2003, 10:19 AM
  4. From Perl to C
    By Heavenstrash in forum C Programming
    Replies: 4
    Last Post: 06-19-2002, 01:22 AM
  5. perl need help pls.....
    By magnum38 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-12-2001, 10:35 PM