The common code is :
use strict;
use warnings;
my $f = $ARGV[0];
use Bio::EnsEMBL::Registry;
use Bio::EnsEMBL::ApiVersion;
my $registry = 'Bio::EnsEMBL::Registry';
$registry->load_registry_from_db(
-host => 'ensembldb.ensembl.org',
-user => 'anonymous',
-port => '5306'
);
my $adaptor = $registry->get_adaptor( 'Homo sapiens', 'core', 'transcript' );
my $transcript =
$adaptor->fetch_by_translation_stable_id($f);
LAST LINE
#
For the LAST LINE, I am having trouble printing out the two values as two columns in the same line :
Attempt 1 code: print $f . $transcript->display_id. "\n";
Result : api.test.pl ENSP00000418690
ENSP00000418690ENST00000488594
Attempt 2 code :print $f, $transcript->display_id. "\n";
Result : perl api.test.pl ENSP00000418690 :
ENSP00000418690ENST00000488594
Any other attempt messes with accessing the display_id. What I want the format to be is :
ENSP00000418690 ENST00000488594
If you want a space between the values, print a space between the values.
print $f, " ", $transcript->display_id, "\n";
The dot joins two strings into one string, which isn't necessary here, because print accepts a comma-separated list of values.
Just to mention another possibility, you can two built-in perl variables.
{
local $, = " "; # Output field separator
local $\ = "\n"; # Output record separator
print $f, $transcript->display_id;
}
The output field separator is automatically printed between each argument in the list passed to print. The output record separator is printed after the final argument to print. Both are undefined by default, and interpreted to mean "don't print anything".
I declared them as local to a block as the simplest way of restricting them to the single print statement.
Related
I have a subroutine called grepText, which simply greps a text from another variable. I am trying to split the output. Is it possible to pass the output of grepText as an argument to split directly? without putting the value of grepText in a variable first ? grepText returns a string.
What i am trying to do is:
$output = (split ":", grepText("findThis", $Alltext))[1];
grepText is as follows
sub grepText(){
my #text = split "\n", $_[1];
my $output = grep /$_[0]/, #text;
return $output;
}
it doesn't work. Error is
Too many arguments for main::grepText at a line 115, near "$Alltext)"
It is very much possible to pass the input of a subroutine to any perl function directly without using a perl variable.
I think the issue might be with your "grepText" subroutine. To debug the issue in detail, much more information is required.
I did try your routine and I was able to get the required output:
#!/usr/bin/perl
sub grepText
{
return "hello:world"; # returns a test string
}
my $output = (split ":", grepText($textToFind, $Alltext))[1];
print "$output";
Output:
world
Sure it is. But as you've written it grepText is getting some strange parameters. In
(split ":", grepText(/$textToFind/, $Alltext))[1];
you're calling grepText(/$textToFind/, $Alltext) which is searching for the value of $textToFind in the global variable $_ and, in list context, is inserting either an empty list () or a list containing 1 (1) into the parameters
So you're calling grepText($Alltext) or grepText(1, $Alltext) depending on whether $_ contains the regex pattern in $textToFind
I'm pretty certain that's not what you want to do, so some more information would be nice!
However, whatever grepText returns will be split on colons : and (split ":", grepText(...))[1] will give you the second colon-separated field, which seems to be what you're asking
I would like to use the value of a variable (fixed by a command line option for instance) as a list separator, enabling that value to be a special character (newline, tabulation, etc.).
Unfortunately the naïve approach does not work due to the fact that the two following print statement behave differentely :
my #tab = ("a","b","c");
# block 1 gives expected result:
# a
# b
# c
{
local $" = "\n"; #" let us please the color syntax engine
print "#tab";
}
# block 2 gives unwanted result:
# a\nb\nc
{
use Getopt::Long;
my $s;
GetOptions('separator=s' => \$s);
local $" = "$s"; #" let us please the color syntax engine
print "#tab";
}
Any idea I can correct the block 2 so that I get the wanted result (the one produced by block 1) ?
It actually does work the same if you assign the same string. Perl's
"\n"
creates a one character string consisting of a newline. With my shell (bash), you'd use
'
'
to do the same.
$ perl a.pl --separator='
'
a
b
ca
b
c
You didn't do this. You passed a string consisting of the two characters \ and n to Perl instead.
If you your program to convert two chars \n into a newline, you'll need to tell it to do so.
my #tab = qw( a b c );
sub handle_escapes {
my ($s) = #_;
$s =~ s/\\([\\a-z])/
$1 eq '\\' ? '\\' :
$1 eq 'n' ? "\n" :
do { warn("Unrecognised escape \\$1"); "\\$1" }
/seg;
return $s;
}
{
my $s = '\n'; #" let us please the color syntax engine
local $" = handle_escapes($s);
print "#tab";
}
{
use Getopt::Long;
my $s;
GetOptions('separator=s' => \$s);
local $" = handle_escapes($s); #" let us please the color syntax engine
print "#tab";
}
$ perl a.pl --separator='\n'
a
b
ca
b
c
I have this perl script and it takes in arguments using the getoption package.
Is there an easy way to document what was the exact command the user used to execute?
I would like to document into a log file.
Gordon
Use $0 and #ARGV together:
my $full_command = join(' ', $0, #ARGV);
or you can simply
my $full;
BEGIN { $full = "$0 #ARGV" }
#
print "log: $full\n";
form the perlvar
$LIST_SEPARATOR
$"
When an array or an array slice is
interpolated into a double-quoted
string or a similar context such as
/.../ , its elements are separated by
this value. Default is a space. For
example, this:
print "The array is: #array\n";
is equivalent to this:
print "The array is: " . join($", #array) . "\n";
Could anyone tell me how could I print values of array in different rows without using loop?
#!/usr/bin/perl -w
my #a = ('Test1','Test2','Test3');
print "#a";# output like **Test1 Test2 Test3** but i want **Test2 in next line and Test3 next to next line**
Is it Possible?
You can just do:
print join("\n", #ar);
You can set $" variable
$" = "\n";
It's probably better to do
{
local $" = "\n";
print "#ar";
}
EDIT:
according to the camel book :
$" (or the alternative $LIST_SEPERATOR) specifies the string to put between individual elements when an array is interpolated into a double-quoted string, this for the case you want to say:
print "#ar";
$, (or the alternative $OUTPUT_FIELD_SEPERATOR) specifies the string to put between individual elements when you want to print a list. It's initially empty. You can set $, for the case you want to say:
print #ar;
You can set the $, special variable to be whatever you want to separate your list elements. This should do what you want:
$, = "\n";
my #a = ('Test1','Test2','Test3');
print #a;
use map function
print #array = map{"$_\n"} #a;
How to print the number of arguments from #ARGV
according to the following script why its important to print
like
print q{don't have parameters};
And not as
print "don't have parameters"; ??
lidia
#!/usr/bin/perl
if (#ARGV) {
print ......
} else {
print q{don't have parameters};
}
To print the number of elements in any array in perl:
print scalar(#ARGV);
Using q{} OR single quotes '' means that a string will get quoted but NOT interpolated, meaning any variables you have inside will not have their actual values. This is a faster way to create strings than with double quotes "" or qq{} which WILL interpolate variables in the string.
moreover, print q{} is a shorthand for :
print 'don\'t have parameters'
double quotes mean your string gets interpolated. ie : perl analysis it to retrieve
variable values.
simple quotes won't. No unrequired analysis -> faster, less memory/cpu/whatever usage