CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Issues with grep

 



pjshort42
Novice

Jul 19, 2012, 9:23 AM

Post #1 of 4 (1203 views)
Issues with grep Can't Post

I have two arrays called nodeA and nodeB and am hoping to match them (using greb, or a better method!) to another array.

The idea is that if a particular row of nodeA is in the other array (nfkbgenes) and the same row of nodeB is also in the array, we will keep the two entries.

When I run it, however, the greb is only returning one of the values of the nfkbgenes in its search (it happens to be the last one, I imagine this is significant). Thank you!

open FILE, "seqmonktop550.txt" or die $!;
my @nfkbgenes = <FILE>;
$sizenfkb = @nfkbgenes;
%nfkbhash = ();
%nfkbhash = map {$_ => 1} @nfkbgenes;
@nfkb_nodeA = ();
@nfkb_nodeB = ();
@nfkb_contacts = ();
$sizenodeA = @nodeA;
for ($i=0; $i<$sizenfkb; $i++)
{
if ((defined $nfkbhash{$nodeA[$i]})and(defined $nfkbhash{$nodeB[$i]})){
print 'success';
push(@nfkb_nodeA, $nodeA[$i]);
push(@nfkb_nodeB, $nodeB[$i]);
push(@nfkb_contacts, $contacts[$i]);


pjshort42
Novice

Jul 19, 2012, 9:38 AM

Post #2 of 4 (1202 views)
Re: [pjshort42] Issues with grep [In reply to] Can't Post

to clarify, that was my attempt using hashes, here is the grep version!

open FILE, "seqmonktop550.txt" or die $!;
my @nfkbgenes = <FILE>;
@nfkb_nodeA = ();
@nfkb_nodeB = ();
@nfkb_contacts = ();
$sizenodeA = @nodeA;
for ($i=0; $i<$sizenodeA; $i++)
{
if ((grep {$_ eq $nodeA[$i]} @nfkbgenes)and(grep {$_ eq $nodeB[$i]} @nfkbgenes)){
push(@nfkb_nodeA, $nodeA[$i]);
push(@nfkb_nodeB, $nodeB[$i]);
push(@nfkb_contacts, $contacts[$i]);
}
}


wickedxter
User

Jul 22, 2012, 4:43 PM

Post #3 of 4 (1181 views)
Re: [pjshort42] Issues with grep [In reply to] Can't Post

you need to put " my " in front of variables so they don't alter or get altered some where outside of its scope. Also useing the modules
ie:

Code
 use strict;  #useing this will help  
use warnings; # As will this help
my @array = qw();


So your openning the file and comparing the info in the file with whats in the array nodeA and nodeB and they have to match in the same place in each?

ie:

Code
my @file =  <FILE>; 
my @nodeA = qw(dog cat perl pig);
my @nodeB = qw(blah dog perl find);


the perl is in the same place in both arrays. But from looking over the code its in compleate. Whats in the arrays nodeA and nodeB?

(also on anouther note these changes are for perl v5.10 and newer:)

Code
open my $file,"<", "seqmonktop550.txt" or die $!; #changed for a 3 arg open 
my @nfkbgenes = <$file>;
my @nfkb_nodeA = ();
my @nfkb_nodeB = ();
my @nfkb_contacts = ();
my $sizenodeA = scalar @nodeA; #makes the intent more clear
for my $i (0..$sizenodeA){ #changed out the 3 arg for. This is more simple.
if ((exists $nfkbhash{$nodeA[$i]}) or (exists $nfkbhash{$nodeB[$i]})){ #it printed matching results with or but and it prints nothing
push(@nfkb_nodeA, $nodeA[$i]);
push(@nfkb_nodeB, $nodeB[$i]);
push(@nfkb_contacts, $contacts[$i]);
}
}



(This post was edited by wickedxter on Jul 22, 2012, 4:44 PM)


pjshort42
Novice

Jul 23, 2012, 12:53 AM

Post #4 of 4 (1158 views)
Re: [wickedxter] Issues with grep [In reply to] Can't Post

Thank you for your help!

The fix was actually as simple as chomping the array before I did the matching, but all of the tips are very helpful!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives