Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Perl script to modify file

Status
Not open for further replies.

kdejan

Programmer
Feb 11, 2013
1
US
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!
 
Hi

[ul]
[li]Please choose an Indent style and use it consistently.[/li]
[li]Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.[/li]
[/ul]

Possibly I misunderstood your task, but for now sounds like it could be reduced to :
Code:
perl -pe '[b]if[/b][teal]([/teal][b]length[/b][navy]$_[/navy][teal]>=[/teal][purple]1480[/purple][teal])[/teal][teal]{[/teal][navy]$e[/navy][teal]=[/teal][b]substr[/b][navy]$_[/navy][teal],[/teal][purple]1417[/purple][teal];[/teal][navy]$e[/navy][teal]=~[/teal][b]s[/b][fuchsia]/a/0/[/fuchsia][b]gi[/b][teal];[/teal][navy]$e[/navy][teal]=~[/teal][b]s[/b][fuchsia]/g/1/[/fuchsia][b]gi[/b][teal];[/teal][b]substr[/b][navy]$_[/navy][teal],[/teal][purple]1417[/purple][teal],-[/teal][purple]1[/purple][teal],[/teal][navy]$e[/navy][teal];[/teal][b]s[/b][fuchsia]/(.)\s/$1/[/fuchsia][b]g[/b][teal]}[/teal]' whatshappenin

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

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top