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:
Help with pattern matching

 



TheDragonReborn
Novice

Nov 4, 2008, 5:46 PM

Post #1 of 3 (263 views)
Help with pattern matching Can't Post

Hello to all,

I am having some problems with pattern matching in my program. What I am trying to do is compare one list against 2 other lists and return values that are common to both or all three. I am relatively new to Perl and unsure what I am doing wrong. I have looked through some books and online tutorials but can't seem to narrow down my error. Here is my code. Hopefully someone can figure out what I am doing


Code
#!/usr/bin/perl 
# A program to compare three list of proteins and
# return a list of those proteins common to all list

# Input file example
# Uetz CCSB Ito
# YOR023C YNL024C YBL039C
# YCR082W YDR386W YJR103W
# YMR092C YMR312W YBL056W
# YLR102C YHR187W YDR071C
# YLR319C YNR051C YBR239C
# YGL015C YER151C YPL133C

$switch = 1 ;

# Get file from user
print "Enter file: " ;
$dataFile = <STDIN> ;
chomp $dataFile ;

# Does file exist?
unless(-e $dataFile)
{
print "File \"$dataFile\" does not seem to exist int this directory!!!" ;
exit ;
}

# Can it open?
unless(open(DATAFILE, $dataFile))
{
print "Error!! Did not read file." ;
exit ;
}

# Variables
@CCSB_data ;
@Ito_data ;
@Uetz_data ;
$i = 0 ;

while($line = <DATAFILE>)
{
# Eliminate white space and break into tokens
@tokens = split(/\s+/, $line) ;

# Create arrays with three lists
$Uetz_data[$i] = $tokens[0] ;
$CCSB_data[$i] = $tokens[1] ;
$Ito_data[$i] = $tokens[2] ;
++$i ;
}

# Turn the CCSB and Ito arrays into long strings so that
# we can search through
$CCSB_string = join('', @CCSB_data) ;
$Ito_string = join('', @Ito_data) ;

#print "\n@Uetz_data\n$CCSB_string\n$Ito_string\n" ;


# Compare three lists and return those common
$x = 1 ;
$y = 0 ;
@data ;
for($x = 1 ; $x < scalar(@Uetz_data) ; ++$x )
{
$test1 = $Uetz_data[$x] ;
#print "$test1\n" ;

if($test1 =~ /$CCSB_string/ or $test1 =~ /$Ito_string/)
{
$data[$y] = $test1 ;
++$y ;
}
}

# Output
print"Select name for output file: " ;
$fileName = <STDIN> ;
chomp $fileName ;

# Write results to output file
$outputFile = "$fileName" ;

unless(open(OUTPUT, ">$outputFile"))
{
print "Cannot open file \"$outputFile\" to write to." ;
exit ;
}

# Print out lines
foreach $row (@data)
{
print OUTPUT "$row" ;
}

close DATAFILE ;
close OUTPUT ;

exit ;


any help would be great


KevinR
Veteran


Nov 4, 2008, 6:20 PM

Post #2 of 3 (259 views)
Re: [TheDragonReborn] Help with pattern matching [In reply to] Can't Post

Does your code run? Does it generate error messages? Does it do something but not what you think it should?

Even though you are new to perl you should complie your perl programs with strict and warnings:

use strict;
use warnings;

you will have to declare your variables with "my" before using them in their intended scope.

It would also help if you could show what you are trying to match across files.
-------------------------------------------------


TheDragonReborn
Novice

Nov 4, 2008, 6:27 PM

Post #3 of 3 (258 views)
Re: [KevinR] Help with pattern matching [In reply to] Can't Post

when I run the code, I get no output. The array @data is empty. The pattern matching statement is not working. At the top of the code is an example of the input. I know for sure that there is at least one item in list 1 that will match with one item in list 3, so the data array should not be empty. I am not getting any error messages.

 
 


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

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