×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help with Perl script to modify file

Help with Perl script to modify file

Help with Perl script to modify file

(OP)
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!

RE: Help with Perl script to modify file

Hi

  • Please choose an Indent style and use it consistently.
  • Next time please post your code between [code] and [/code] TGML tags.
Possibly I misunderstood your task, but for now sounds like it could be reduced to :

CODE --> command-line

perl -pe 'if(length$_>=1480){$e=substr$_,1417;$e=~s/a/0/gi;$e=~s/g/1/gi;substr$_,1417,-1,$e;s/(.)\s/$1/g}' whatshappenin 

Regarding your mistake, the == operator does numeric comparison. Use eq to compare strings. ( See Equality Operators in man perlop for details. )

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close