Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Extract line, matching more than one variable

by yads25 (Initiate)
on Nov 12, 2008 at 12:00 UTC ( [id://723123]=perlquestion: print w/replies, xml ) Need Help??

yads25 has asked for the wisdom of the Perl Monks concerning the following question:

Need to write a perl code to extract a line from a file <file2> that matches the several <say 3> values from columns of another file <file1>
  • Comment on Extract line, matching more than one variable

Replies are listed 'Best First'.
Re: Extract line, matching more than one variable
by prasadbabu (Prior) on Nov 12, 2008 at 12:20 UTC
      Hi I tried the code....need debuging could you pl help..thank you.
      #!/usr/bin/perl -w $sasa_file= 'File1'; $pdbfile='File2'; # Remove the newline from the DNA filename open (MYXFILE,$sasa_file); open (PDBFILE, $pdbfile); while (<MYXFILE>){ chomp; @sa = split(/\t/,$_); #print "$sa[0] $sa[1] $sa[2]\n"; #$res = substr $_, 0, 4; #$res_no = substr $_, 4, 5; #$atom =substr $_, 9, 3; while (<PDBFILE>){ chomp; $res = substr $_, 17, 3; $res_no = substr $_, 22, 4; $atom =substr $_, 13, 3; #print " $res $res_no $atom \n" ; # works fine if (( $sa[0]eq$res)&&($sa[1]==$res_no)&&($sa[2] eq $atom)) { print "$_"; # does not print anything. } } } close MYXFILE; close PDBFILE;

        Hi yads25,

        Please use <code> tags to wrap the coding parts in your nodes.

        Can you please show us your sample file, so that we can give solutions without assumption.

        Prasad

Re: Extract line, matching more than one variable
by ww (Archbishop) on Nov 12, 2008 at 12:48 UTC

    So?

    So just write it.

    ...oh, you hoped we would write it for you?

    Wrong!

    So, perhaps you should use Search (entering "extract a line" in the "Search" box above produces useful results) or Super_Search for the answers to this oft-repeated question. And if that doesn't avail, you should definitely show a bit of effort (i.e. show us some code).

    See, please How do I post a question effectively?, On asking for help and, not just incidentally, Writeup Formatting Tips.

    Welcome to the Monastery. Reading the tips above will warm that welcome.

Re: Extract line, matching more than one variable
by Numbski (Initiate) on Nov 12, 2008 at 14:38 UTC

    So - the guys above said their piece, but just for my own 2 cents, there's value to be had from this exercise, because doing multi-matching without getting too loopy is tricky.

    So from your question, you have two filehandles, FILE1 and FILE2. You need to exract "a line" from FILE2 "that matches several values" from FILE1. So we need to presume you've already hit the matching conditions to know what you're looking for from FILE2.

    Which is interesting, because if you know what the value is, then it gets reduced to:

    (This is not valid, just giving you the idea)
    FILE2 =~ s/my stuff to get removed//g;

    In short, you need to give more information than you just did, because logically following what you wrote, you're asking us how to delete lines from a file that match a condition, which results in the above pseudo-code.

      There are two files, File 1 and File 2. Both have data in columns. Three variables from File 1, var1, var2 and var3 need to match a single line of file2 and that line needs to be extracted and appended in 'results file'. Wrote following code...could some one debug it for me. #!/usr/bin/perl -w #open file f-2 #print "Please type the filename of the data: "; $sasa_file= 'File1'; $pdbfile='File2'; # Remove the newline from the DNA filename open (MYXFILE,$sasa_file); open (PDBFILE, $pdbfile); while (<MYXFILE>){ chomp; @sa = split(/\t/,$_); #print "$sa[0] $sa1 $sa2\n"; #$res = substr $_, 0, 4; #$res_no = substr $_, 4, 5; #$atom =substr $_, 9, 3; while (<PDBFILE>){ chomp; $res = substr $_, 17, 3; $res_no = substr $_, 22, 4; $atom =substr $_, 13, 3; #print " $res $res_no $atom \n" ; # works fine if (( $sa[0]eq$res)&&($sa1==$res_no)&&($sa2 eq $atom)) { print "$_"; # does not print anything. } } } close MYXFILE; close PDBFILE;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://723123]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-24 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found