#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    63
    Rep Power
    3

    Match entires and seaprate by bracket before writing


    Hi all,

    I have a big file like this kind of data in two columns


    Code:
    benzene blood disorders
    nitrozen air disease

    I have another file like this in different columns


    Code:
    AXY benzene, nitrogen, oxygen, not available, sulfur
    VTY nitrogen, notavailable, sulfur
    I want from second columnonwards of second file it should match the first column of first file so that and if mathced right in fornt of it whatever is written.
    Exoected output is


    Code:
    AXY benzene (blood disorders), nitrogen (air disease), oxygen, not available, sulfur.
    VTY nitrogen (air disease), notavailable, sulfur
  2. #2
  3. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    what do you expect to happen here?
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    63
    Rep Power
    3

    Request to check


    Hi

    Thanks for reply.

    I am looking for guidance regarding writing a perl program for such a question as I m novice in Perl programming as I am from Life science background and I need this for some science research.

    Mani
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,938
    Rep Power
    1225
    Hello Mani,

    We would be happy to help you with the troubleshooting of your code, however, based on your prior postings that I've read, you're looking for someone to write your entire script for you or at least the vast majority of it. If that's the case, then it would be best to hire a Perl programmer. If I'm wrong, then please post your script and a specific question on the part that is giving you trouble. Once we have that info, we can begin to give help.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,938
    Rep Power
    1225
    I was needing a diversion from my own work, so I went ahead and worked up a minimal script for you.

    Code:
    use strict;
    use warnings;
    
    my $file1 = 'file1.txt';
    my $file2 = 'file2.txt';
    
    open my $f1_fh, '<', $file1 or die "failed to open $file1 $!";
    open my $f2_fh, '<', $file2 or die "failed to open $file2 $!";
    
    my %data;
    
    while ( my $line = <$f1_fh> ) {
        chomp $line;
        my ($key, $value) = split /\s+/, $line, 2;
        $data{$key} = $value;
    }
    close $f1_fh;
    
    while ( my $line = <$f2_fh> ) {
        chomp $line;
        my ($key, $value) = split /\s+/, $line, 2;
        my @parts = map { $data{$_} ? "$_ ($data{$_})" : $_ } split /, /, $value;
        
        printf "%s\t%s\n", $key, join(', ', @parts);
    }
    close $f2_fh;
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    63
    Rep Power
    3

    Request to check


    Hi

    Thanks for the reply.

    I used this programm. May be I m doing soemthign worng with file 1 and file2 naming as I named my input files as file 1 and file 2 itself

    or

    May be I have to run the programm in differnet way

    and it shows such errors as well.

    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65525.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65526.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65527.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65528.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65529.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65530.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65531.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65532.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65533.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65534.
    Use of uninitialized value in hash element at pharmgkbtest.pl line 15, <$f1_fh> line 65535.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,938
    Rep Power
    1225
    Obviously one or both of your data files is not formatted as you've presented to us.

    Please post both of your scripts as attachments, not copy/pasted inline, so that we can see the precise format.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    You probably have empty lines (or lines not having two fields) at the end of your file 1.

    If this is the case, you can fix it by changing the relevant line:

    Perl Code:
    $data{$key} = $value if defined $value;

IMN logo majestic logo threadwatch logo seochat tools logo