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: Intermediate:
if else equality question

 



kdejan
New User

Feb 11, 2013, 4:27 PM

Post #1 of 3 (688 views)
if else equality question Can't Post

Hi all,

I'm writing a program to replace A's and G's in a text file I have. This is the code I have:

#!usr/bin/perl

open(FILE, "whatshappenin") or die $!;

#QUICKEST WAY OF READING A LINE BY LINE INTO AN ARRAY
foreach $line (<FILE>)
{
chomp($line);

my @array_of_chars = split(//,$line);
my $array_of_charsSize = $#array_of_chars + 1;
if($array_of_charsSize < 1480)
{
#THIS PRINTS THE SEQUENCE TITLES
print"$line\n";
}else{
#LOOP THROUGH SEQUENCE DATA ONE CHARACTER AT A TIME
for($i=0;$i<$array_of_charsSize;$i++)
{
#ONLY EDIT ONCE YOU REACH GAP PARTITION (1416 FOR TRN)
if($i>1416)
{
#CHANGE PRESENCE OF DATA 'A' TO 1
if($array_of_chars[$i] == "A" || $array_of_chars[$i] == "a")
{
$array_of_chars[$i] = 0;
#CHANCE PRESENCE OF GAP 'G' TO 0
}elsif($array_of_chars[$i] == "G" || $array_of_chars[$i] == "g")
{
$array_of_chars[$i] = 1;
}
}
}
#PRINT EDITED SEQUENCE
$str = "@array_of_chars";
$str =~ s/(.)\s/$1/seg;
print"$str\n";

}
}

Unfortunately it does not recognize any G's and just changes all the characters to 0's once $i>1416. I have a feeling it has to do with the equality statement inside my ifelseif statements, but I can't figure it out. Do I need to use ASCII values or something?

Any help is much appreciated!


BillKSmith
Veteran

Feb 11, 2013, 8:53 PM

Post #2 of 3 (682 views)
Re: [kdejan] if else equality question [In reply to] Can't Post

The operator '==' is for comparing numbers. Use the operator 'eq' to compare strings.

I have several other recommendations.

  • Always use strict and warnings.

  • Always use the three argument form of open.

  • Always use lexical (my) file handles.
  • Always explicitly close all open files.

  • Process each line of this file as a string. There is no reason to split it into a character array.

    Update: Added sample code (Untested due to lack of data)

    Code
    use strict; 
    use warnings;
    use Readonly;
    Readonly::Scalar my $GAP_PARTITION => 1417;
    Readonly::Scalar my $MAX_TITLE_SIZE => 1480;
    open( my $FILE, '<', "whatshappenin" ) or die $!;
    foreach my $line (<FILE>) {
    chomp $line;
    if ( length $line < $MAX_TITLE_SIZE ) { print $line ; next; }
    (my $temp = substr( $line, $GAP_PARTITION )) =~ tr /AaGg/0011/;
    substr($line, $GAP_PARTITION, length $temp, $temp);
    $line =~ s/(.)\s/$1/seg;
    print "$line\n";
    }
    close $FILE;

    Good Luck,
    Bill

    (This post was edited by BillKSmith on Feb 12, 2013, 7:48 AM)


  • Kenosis
    User

    Feb 24, 2013, 6:11 PM

    Post #3 of 3 (620 views)
    Re: [kdejan] if else equality question [In reply to] Can't Post

    Here's another option:


    Code
    use strict; 
    use warnings;

    while (<>) {
    chomp;

    if ( length $_ < 1480 ) {
    print "$_\n";
    next;
    }

    s/(.{1415})//;
    tr/AaGg/0011/;

    print "$1$_\n";
    }


    Usage: perl script.pl inFile [>outFile]

    The substitution matches and captures the first 1415 characters of the read line (contained in the default scalar $_), then removes them. The variable $1 now contains the pre-gap string, and $_ contains the post-gap string. A transliteration (implicitly) operates on $_, and then the pre-gap and post-gap strings are printed.

    The optional command-line parameter, if used, redirects output to a file.


    (This post was edited by Kenosis on Feb 24, 2013, 6:19 PM)

     
     


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

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