perltidy formatting multilines - perl

I'm trying to get perltidy to format an if statement like this:
if ($self->image eq $_->[1]
and $self->extension eq $_->[2]
and $self->location eq $_->[3]
and $self->modified eq $_->[4]
and $self->accessed eq $_->[5]) {
but no matter what I try, it insists on formatting it like this:
if ( $self->image eq $_->[1]
and $self->extension eq $_->[2]
and $self->location eq $_->[3]
and $self->modified eq $_->[4]
and $self->accessed eq $_->[5]) {
Also, is there any way to get the last line of this block:
$dbh->do("INSERT INTO image VALUES(NULL, "
. $dbh->quote($self->image) . ", "
. $dbh->quote($self->extension) . ", "
. $dbh->quote($self->location) . ","
. $dbh->quote($self->modified) . ","
. $dbh->quote($self->accessed)
. ")");
to jump up to the previous line like the other lines:
$dbh->do("INSERT INTO image VALUES(NULL, "
. $dbh->quote($self->image) . ", "
. $dbh->quote($self->extension) . ", "
. $dbh->quote($self->location) . ","
. $dbh->quote($self->modified) . ","
. $dbh->quote($self->accessed) . ")");
Here is what I'm currently doing:
perltidy -ce -et=4 -l=100 -pt=2 -msc=1 -bar -ci=0 reporter.pm
Thanks.

I don't have much to offer on the 1st question, but with the 2nd, have you considered refactoring it to use placeholders? It would probably format up better, automaticaly do the quoting for you and give you (and the users of your module) a healthy barrier against SQL injection problems.
my $sth = $dbh->prepare('INSERT INTO image VALUES(NULL, ?, ?, ?, ?, ?)');
$sth->execute(
$self->image, $self->extension, $self->location,
$self->modified, $self->accessed
);
I've also found format skipping: -fs to protect a specific segment of code from perltidy. I'd put an example here but the Site seems to do a hatchet job on it...

Related

Issue generating .xls since changing from PHP 5 to PHP 7

I've recently switched from PHP 5.6 to PHP7. A previous PHP script which worked fine in PHP 5.6 is now duplicating columns data in PHP 7. I'm not seeing why. The code:
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\t", $str);
$str = preg_replace("/\r?\n/", "\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
// force certain number/date formats to be imported as strings
if(preg_match("/^0/", $str) || preg_match("/^+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str))
{
$str = "'$str";
}
}
$day_num = date(d);
$month_num = date(n);
$year_num = date(Y);
// file name for download
$filename = "Subscriber_list_" . $month_num . "-" . $day_num . "-" . $year_num . ".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment;
filename=\"$filename\"");
header('Cache-Control: max-age=0');
echo "ID \t Title \t First Name \t Last Name \t E-Mail \n";
$news_query = mysqli_query("SELECT DISTINCT id, title,firstname, lastname, email FROM users WHERE newsletter = 1 ");
while($row =mysqli_fetch_array($news_query, MYSQL_ASSOC)) {
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\n";
}
exit;
The DB is fairly standard. Thanks in advance.
I think this is because you're using the MYSQL_ASSOC constant instead of MYSQLI_ASSOC when you fetch rows from the query.
That typo happened to work when you were on PHP 5, because those two constants have the same value. But the MYSQL_ASSOC constant is undefined in PHP 7 since the MYSQL extension has been removed, so mysqli_fetch_array goes with the default behavior of fetching both numeric and associative keys, hence the duplicated columns.
This is solved, Thanks.
I re-wrote the script using:
<pre>
function fetch_assoc($result) {
return mysqli_fetch_assoc($result);
}
</pre>

Nested "if", Missing right curly or square bracket

I currently have the following:
elsif ($line =~ /^(\s*)(if|elif|else)\s*(.+)*\s*:\s*$/) {
# Multiline If
# Print the If/Elif condition
if ($2 eq "if"){
print "$1$2 ($3){\n";
}
elsif ($2 eq "elif"){
print "$1elsif ($3){\n";
}
elsif ($2 eq "else"){
print "$1$2 $3{\n";
}
# Add the space before the word "if"/"elif"/"else" to the stack
push(#indentation_stack, $1);
}
I am getting the stated error, but I'm not sure why. In the final elsif, if I add a \ before the { in the print statement, the code doesn't generate an error.
I.E:
elsif ($2 eq "else"){
print "$1$2 $3\{\n";
}
Could someone please explain to me why this is occurring?
Thanks for your help!
Tricky! The problem is that the following is the start of hash lookup:
$3{
You want the equivalent of
$3 . "{"
which can be written as
"${3}{"
The following works in this case because the \ can't possibly be part of the variable there:
"$3\{"
But that trick can't always be used. For example, consider
$foo . "bar"
If you try
"$foo\bar"
You'll find you get
$foo . chr(0x08) . "ar"
because "\b" returns the "bell" character. That leaves you with
"${foo}bar"

counter in php do while loop

I am checking ( or want to check ) if the counter gets to 5 I want to reset the counter back to one and start again until all the records have been read,I have the following code on a test page:
<?php do {
if ($count == 5) { $count = 1;}
echo $count;
echo "<div id=\"col$count\"><div id=\"col$count-content\"><img class=\"resetImg\" name=\"colpic$count\" src=\"assets/images/student_artwork/thumb_" . $row_rsILU['ilu_artwork'] . "\" alt=\"\" style=\"background-color: #999966\"><br><br><span class=\"artistName\">" . $row_rsILU['ilu_fname'] . " " . $row_rsILU['ilu_lname'] . "</span> <br><hr>
Concept Artist<br>
email#artistdomain.ca<br>
416-833-1111<br>
www.portfolio.ca<br>
</div></div>";
$count++;
} while ($row_rsILU = mysql_fetch_assoc($rsILU)); ?>
The record set has three records in it but the count always seems to stop at two and only shows the two images. I am checking the count and if it gets to 5, I want to reset it to 1 and start over again until the total records in the record set have been read. Maybe I am just getting tired but I cannot get this. Would appreciate anyones help on this.
Cheers,
Dave
According to your code, you are trying to echo the row before actually fetching it (I mean the first iteration). Why not just use while loop instead?
<?php
while ($row_rsILU = mysql_fetch_assoc($rsILU)) {
if ($count == 5) { $count = 1;}
echo $count;
echo "<div id=\"col$count\"><div id=\"col$count-content\"><img class=\"resetImg\" name=\"colpic$count\" src=\"assets/images/student_artwork/thumb_" . $row_rsILU['ilu_artwork'] . "\" alt=\"\" style=\"background-color: #999966\"><br><br><span class=\"artistName\">" . $row_rsILU['ilu_fname'] . " " . $row_rsILU['ilu_lname'] . "</span> <br><hr>
Concept Artist<br>
email#artistdomain.ca<br>
416-833-1111<br>
www.portfolio.ca<br>
</div></div>";
$count++;
}
?>
Please let me know if that solves your problem.
Humm ... well now, this is embarrassing! Turns out there was a second, older SELECT statement in the code that I missed and so it was not returning one of the three records because it was not looking for it, based on the older SELECT statement. Sorry about that but thanks for your help.

preg_replace new statement to find variables

Hi at the moment I use preg_replace to replace a variable like "$money" with a text. Now I have changed my vars to "%%money%%" what I must changed in the preg_replace statement?
preg_replace("#\\$" . $var . "([^a-zA-Z_0-9\x7f-\xff]|$)#", $value . "\\1", $text);
How about:
preg_replace("#%%" . $var . "([^a-zA-Z_0-9\x7f-\xff]?)%%$#", $value . "$1", $text);

How to remove fasta formatted sequences that contain Ns

I have a fasta file like such
">ENS..._intronX
acgtacgtacgtacgt
">ENS..._intronY
acgtacgtNNNNa
acgtacgtacgtacgt
">ENS..._intronZ
acgtacgtacgtacgt
acgtacgtacgtacgt
I need to remove sequences with at least 2 N in a row (because these introns are misannotated).
Here, it would be sequence " >ENS..._intronY " (Line 3, 4, and 5 should be removed)
any suggestions?
Thank you,
With gawk
awk -v RS='">' '!/NN/{printf $0RT}' file
">ENS..._intronX
acgtacgtacgtacgt
">ENS..._intronZ
acgtacgtacgtacgt
acgtacgtacgtacgt
Since it appears you're pursuing bioinformatics, consider becoming familiar with Bio::SeqIO, as it'll help with this and many other fasta parsing jobs:
use strict;
use warnings;
use Bio::SeqIO;
my $in = Bio::SeqIO->new( -file => shift, -format => 'Fasta' );
while ( my $seq = $in->next_seq() ) {
print '>' . $seq->id . ' ' . $seq->desc . "\n" . $seq->seq . "\n"
if $seq->seq !~ /nn/i;
}
Usage: perl script.pl inFile [>outFile]
The last, optional parameter directs output to a file.
Output on your dataset:
>ENS..._intronX
acgtacgtacgtacgt
>ENS..._intronZ
acgtacgtacgtacgtacgtacgtacgtacgt
Hope this helps!