1.11M Members

how can change two value become one value?

 
0
 

Hi all,

I have one problem. I have two value ($gia and $name1) and now I want to do it become one value to run in the next step.Ex:

$gia =12 $name = Tai
$gia =$gia.$name
out put : 12Tai

But I hope the data will sepera two colums for the next step.

12 Tai

Could you show me how can I do that in perl?

 
0
 
#!/usr/bin/perl
use strict; 
use warnings; 

my $gia = '12tai';

#Capture sequence of digits into one variable,
#capture sequence of non-digits into another variable
my ($number, $name) = $gia =~ m/(\d+)(\D+)/;

print "$number $name\n";#Two values separated by one space
 
0
 

Hi d5e5. I think biojet wants opposite.
biojet, try this one:

my $first = 12;
my $second = "Tai";
my $oneval = "$first$second";
say $oneval;
 
0
 

Thanks d5e5!
But I want to use the script that you show me at the last question http://www.daniweb.com/software-development/perl/threads/413279
It has line

read_positions('data1.txt');

But I did not put out the data1.txt I hope to insert dicrecly two value $gia and $name1 into your script so I try to do two value become one value to insert into the line read_positions($gia) with $gia=$gia.$name1 but it is not secceessful.
Could you show me how to solve this problems?
Thank you so much.

#!/usr/bin/perl
use strict; 
use warnings; 

my $gia = '12tai';

#Capture sequence of digits into one variable,
#capture sequence of non-digits into another variable
my ($number, $name) = $gia =~ m/(\d+)(\D+)/;

print "$number $name\n";#Two values separated by one space
 
0
 

Thanks neo so much! I tried it but I am not seccessful.
when I used the $oneval in my script It has foul I think beacause I can not sepeare two value.

Hi d5e5. I think biojet wants opposite.
biojet, try this one:

my $first = 12;
my $second = "Tai";
my $oneval = "$first$second";
say $oneval;
 
0
 

can you post error message here?

 
0
 

The erro: Failed open 6114067_contig34:No such file or directory at c:\users\PCUSER\desktop\thu.pl line 129
Below is my script I sorry it is long.
Would you show me how to mix this error?

#!/usr/bin/perl;
use strict;
use warnings;


my %aas; #Hash to store amino acids
my $out = 'result.txt';
my $filename3 = 'Found_out.txt';
my $filename4 = 'output1.txt';
my $filename5 = 'Not_found.txt';
my %ranges;
my ($ref1,$base,$num3,@ref1, $posi1,$DNA,@posi1, $posi2);
print 'ENTER THE DNA SNP POSITION := '; 
     	my $filename1 = <STDIN>; 
	chomp $filename1;
	unless (open(DNAFILE, $filename1)) 
	{ 
    	die 'Cannot open file "' . $filename1 . '"' . "\n\n"; 
	} 
print 'ENTER THE DNA OF GENLIST := '; 
     	my $filename2 = <STDIN>; 
	chomp $filename2;
	unless (open(DNAFILE, $filename2)) 
	{ 
    	die 'Cannot open file "' . $filename2 . '"' . "\n\n"; 
	} 
print 'ENTER THE DNA SNP POSITION := '; 
     	my $aa= <STDIN>;
	chomp $aa;
	unless (open(DNAFILE, $aa)) 
	{ 
    	die 'Cannot open file "' . $aa . '"' . "\n\n"; 
	} 
print "デターを修理していますのでしばらくお待ちください \n"; 
open my $fh, '<', $filename2 or die "Failed to open $filename2: $!";
while (<$fh>){
    next unless m/^\d/; #skip unless line starts with number
    chomp;
    my ($num, $start, $end, $name , $ORF, $product) = split;
    $ranges{$num}{Star_posi} = $start;
    $ranges{$num}{End_posi} = $end;
    $ranges{$num}{'Name'} = $name;
    $ranges{$num}{'ORF Name'} = $ORF;
    $ranges{$num}{'Product'} = $product;
    
}
close $fh;

open $fh, '<', $filename1 or die "Failed to open $filename1: $!";
open my $fho1, '>', $filename4 or die "Failed to open $filename4: $!";#Open file for output1
open my $fho2, '>', $filename5 or die "Failed to open $filename5: $!";#Open file for output1
open my $fho, '>', $filename3 or die "Failed to open $filename3: $!";#Open file for output
print $fho "Name\tgen\tProduct\tSNP_posi\tstar\tend\tSNP_site\tRef_aa\tRef\tmut\tTripret_1\n";
print $fho1 "SNP\tSNP_site\tRef_base\tRef\tname\tgen\tProduct\n";

while (<$fh>){
    next unless m/^\d/; #skip unless line starts with number
    chomp;    
    my ($num, $posi, $mut, $ref) = split;
    my $range_num = search_ranges($posi); 
 
    if ($range_num){
        #print to output file instead of STDOUT
        my $name1=$ranges{$range_num}{'Name'} ;
        my $ORF1=$ranges{$range_num}{'ORF Name'} ;
        my $proc1=$ranges{$range_num}{'Product'};
        my $mod = (($posi -$ranges{$range_num}{Star_posi} )% 3) + 1;
        my $int = int(($posi -$ranges{$range_num}{Star_posi})/3);
        my $gia =($int*3)+$ranges{$range_num}{Star_posi};         
    
     # tim aa tu chuoi DNA
	read_amino_acids ($aa); 

     # I have problem in here

          my $position= "$gia$name1";      
          my @position1 =$position;    
          my ($position1) =join ("",@position1);      
          read_positions ($position1);      
       
     

        print $fho1 "$posi\t$mod\t$mut\t$name1\t$ORF1\t$proc1\n";
        print $fho " $name1\t$ORF1\t$proc1\t$posi\t$ranges{$range_num}{Star_posi}\t$ranges{$range_num}{End_posi}\t$mod\t$ref\t$mut\t$gia\t";
   }
    else{
        print $fho2 "SNP record number $posi not found in Genlist\n";
    }
   

}
   print "\nデターはFound_out.txtファイルに保存しました。このファイルをご確認下さい。\n";
   print "次のStepは120127_pro_basechangeファイルを利用してください。\n";
   
 sub search_ranges{
    my $pos = shift;
    foreach (keys %ranges){
        return $_ if $pos >= $ranges{$_}{Star_posi}
                    and $pos <= $ranges{$_}{End_posi};
    }
}


sub read_amino_acids{
    my ($filename) = @_;
    open my $fh, '<', $filename or die "Failed to open $filename: $!";
    my ($name, $key);
    while (<$fh>){
        s/\s+$//; #Remove end-of-line characters
        my @flds = split /\|/;
        if (@flds > 1){
            $name = $flds[4];
            $name =~ m/(\d+)\s(\w+\d\d)/;
            $key = lc("$1_$2");
            undef $aas{$key} unless exists $aas{$key};
        }
        else{
            $aas{$key} .= $_;
        }
    }
}



sub read_positions{
    my ($filename) = @_;
    open my $fh, '<', $filename or die "Failed to open $filename: $!";
   

    while (<$fh>){
        s/\s+$//; #Remove end-of-line characters
        my ($name, $pos) = split;
        next unless $name =~ m/^\d+_/;
        my $tuple = substr $aas{$name}, $pos - 1, 3;
        printf  $fho1 "$tuple\n";
      
    }
}

can you post error message here?

Attachments genlist.txt (135.67KB) posi.txt (9.86KB) data2.txt (0.84KB)
 
0
 

You want to put the content of $gia and $name together to have the name of an existing file that you can pass to a subroutine which will open and read that file, right? I don't know why you would ask how to separate that file name into columns, but the following is an example of passing a variable containing one scalar value that should be a valid file name.

#!/usr/bin/perl
use strict; 
use warnings; 

my $gia = 12;
my $name = 'tai';

my $oneval = $gia.$name; #Name of file should be '12tai', right?

print_first_argument($oneval);#Call subroutine and pass one scalar value as argument

sub print_first_argument{
    my ($filename) = @_;#Assign argument to lexical variable
    print "The first argument is '$filename'\n";
    #If $filename contains the name of a file that exists 
    #then you should be able to open and read it.
}

If my ($filename) = @_;#Assign argument to lexical variable looks confusing you can substitute with my $filename = shift(@_);#Assign argument to lexical variable which does exactly the same thing.

 
0
 
my $position="$gia$name1";      
read_positions ($position);

try above changes

 
0
 

Hi neo!
thanks for your comment!
I had used the same it but it all have the same error.

my $position="$gia$name1";      
read_positions ($position);

try above changes

 
0
 

That file you want to read is in the directory with your perl script?
Because you give only file name (without path) to read_positions() routine...

 
0
 

Because I hope to use two value $gia and $name1 in my script. And I tried to use

my $position="$gia$name1";
    read_positions ($position);

but it has error to read file.

The erro: Failed open 6114067_contig34:No such file or directory at c:\users\PCUSER\desktop\thu.pl line 129

I do not know how to solve that error. Could you show me how to solve this error?

You want to put the content of $gia and $name together to have the name of an existing file that you can pass to a subroutine which will open and read that file, right? I don't know why you would ask how to separate that file name into columns, but the following is an example of passing a variable containing one scalar value that should be a valid file name.

#!/usr/bin/perl
use strict; 
use warnings; 

my $gia = 12;
my $name = 'tai';

my $oneval = $gia.$name; #Name of file should be '12tai', right?

print_first_argument($oneval);#Call subroutine and pass one scalar value as argument

sub print_first_argument{
    my ($filename) = @_;#Assign argument to lexical variable
    print "The first argument is '$filename'\n";
    #If $filename contains the name of a file that exists 
    #then you should be able to open and read it.
}

If my ($filename) = @_;#Assign argument to lexical variable looks confusing you can substitute with my $filename = shift(@_);#Assign argument to lexical variable which does exactly the same thing.

 
0
 

Because I hope to use two value $gia and $name1 in my script. And I tried to use

my $position="$gia$name1";
    read_positions ($position);

but it has error to read file.

The erro: Failed open 6114067_contig34:No such file or directory at c:\users\PCUSER\desktop\thu.pl line 129

I do not know how to solve that error. Could you show me how to solve this error?

That message says that a file named '6114067_contig34' does not exist in your current directory. Either the file named '6114067_contig34' is in a different directory or it has a different name. Should it have an extension (such as dot something). For example if the file is really named '6114067_contig34.txt' then trying to open '6114067_contig34' will result in the error message you are getting.

Do you know what the current directory is when perl runs your scripts? What is the output when you run the following?

#!/usr/bin/perl
use strict; 
use warnings; 

use Cwd qw();
my $path = Cwd::cwd();
print "$path\n";

my @files = <6114067_contig34*>;

foreach my $file (@files) {
    print $file . "\n";
}

print "No files exist starting with '6114067_contig34' in $path\n" if @files == 0;
 
0
 

Thanks d5e5!
I fix the problem and it run well.

That message says that a file named '6114067_contig34' does not exist in your current directory. Either the file named '6114067_contig34' is in a different directory or it has a different name. Should it have an extension (such as dot something). For example if the file is really named '6114067_contig34.txt' then trying to open '6114067_contig34' will result in the error message you are getting.

Do you know what the current directory is when perl runs your scripts? What is the output when you run the following?

#!/usr/bin/perl
use strict; 
use warnings; 

use Cwd qw();
my $path = Cwd::cwd();
print "$path\n";

my @files = <6114067_contig34*>;

foreach my $file (@files) {
    print $file . "\n";
}

print "No files exist starting with '6114067_contig34' in $path\n" if @files == 0;
Question Answered as of 2 Years Ago by _neo_ and d5e5
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article