mysqli commands using ' - mysqli

I have been following a tutorial to better understand php's oop, but have come across a stumbling block, im not sure if it's an oop thing or something that i just haven't come across in the procedural way of doing things, i'm pretty new and trying to learn a lot quickly. I like to know what's happening before moving on.
Below is the code, which I understand apart from why under users_username i need ', and another ', before selecting users_email? At first I thought it was a way of separating them so as a test I removed them and was returned the error: Trying to get property of non-object. So I am guessing by removing them i am somehow stopping the name being created as an object? or am I way off the mark.
"SELECT CONCAT(users_username,
',
',
users_email)
AS name,
DATE_FORMAT(users_joined, '%M %d, %Y') AS dr
FROM users
ORDER BY users_joined
ASC";
$r = $mySqli->query($q);//run the query
$num = $r->num_rows;//assign the number of rows
if($num > 0){//if there are members in the database
echo '<p>There are currently ' . $num . ' members registered.</p>' . "\n";
while($row = $r->fetch_object()){
echo $row->name . ' ' . $row->dr . "<br/>";
}
}
Apologies if this has been asked many times before but I didn't know how to search this question.

What CONCAT(users_username, ',',users_email) is doing is taking the users_username appending a comma and then appending the users_email. So if users_username is bob and users_email is bob#example.com that will return "bob,bob#example.com".
If you remove the single quotes then it is just CONCAT(users_username,,,users_email) and PHP might not like that there is nothing between the commas. In this case the query may be failing and returning NULL for $row. So when you do $row->name it tries to get the name property of $row but since $row is NULL it doesn't have any properties.

Related

PHP do 2 preg_replace in link tag

I am trying to make a multi preg_replace, not sure if thats the correct function.
I want this outcome
[link]www.mynewhomepage.com(My new homepage)[/link]
to become <a href=mynewhomepage.com>My New homepage</a>
I have made this code, which dosent give me what i want
$string = 'i have made a new homepage visit [link]http://myhomepage.dk(My New homepage)[/link]';
$find = array('#\[link\](.+)\[\/link\]#iUs', '#\((.+)\)#iUs');
$replace = array('<a href=$1>', '</a>');
$result = preg_replace($find, $replace, $string);
echo $result;
And it give me this outcome: http://myhomepage.dk>
Can anyone guide me or help me in the right direction of what i am doing wrong? :)
Thanks and happy summer for you :)
This should work:
\[link\](.*?)\((.*)\)\[\/link\]
https://regexr.com/3sc23
It basically matches up to first left parenthesis and also from last parenthesis to the end. Then you put these pieces into capturing groups for referencing them later.
Use $2 as substitution
About your original question, your solution had these problems:
For first replacement we should use <a href="$1. Note that we use " at the beginning of the link but for the moment we do not add it at the end. That way, the nest regexp will be easier.
Then, on the second regexp we should add "> at the beginning in order to close the tag. Also you were not using the captured group at all. That would be the replacement: ">$1</a>
That is, change this line:
$replace = array('<a href=$1>', '</a>');
to this:
$replace = array('$1');

perl to hardcode a static value in a field

I am still learning perl and have all most got a program written. My question, as simple as it may be, is if I want to hardcode a string to a field would the below do that? Thank you :).
$out[45]="VUS";
In the other lines I use the below to define the values that are passed into the `$[out], but the one in question is hardcoded and the others come from a split.
my #vals = split/\t/; # this splits the line at tabs
my #mutations=split/,/,$vals[9]; # splits on comma to create an array of mutations
my ($gene,$transcript,$exon,$coding,$aa);
for (#mutations)
{
($gene,$transcript,$exon,$coding,$aa) = split/\:/; # this takes col AB and splits it at colons
grep {$transcript eq $_} keys %nms or next;
}
my #out=($.,#colsleft,$_,#colsright);
$out[2]=$gene;
$out[3]=$nms{$transcript};
$out[4]=$transcript;
$out[15]=$coding;
$out[17]=$aa;
Your line of code: $out[45]="VUS"; is correct in that it is defining that 46th element of the array #out to the string, "VUS". I am trying to understand from your code, however why you would want to do that? Usually, it is better practice to not hardcode if at all possible. You want to make it your goal to make your program as dynamic as possible.

To find the matched and unmatched values in perl

I am a newbie to programming and I hope someone can explain this to me:
So I have two text files i.e. Scan1.txt and Scan2.txt that are stored in my computer. Scan1.txt contains:
Tom
white
black
mark
john
ben
Scan2.txt contains:
bob
ben
white
gary
tom
black
patrick
I have to extract the matched values of these two files and the unmatched values and print them separately. I somehow found the solution for this which works fine. But can someone please explain how exactly the match happens here. Looks like somehow just this line:
$hash{$matchline}++ in the code does the matching and increments the value of hash when the match is found. I understand the logic but I do not understand how this match actually happens. Can someone help me understand this?
Thank you in advance!
Here is the code:
open (F1, "Scan1.txt");
open (F2, "Scan2.txt");
%hash=();
while ($matchline= <F1> ){
$hash{$matchline}=1;
}
close F1;
while( $matchline= <F2> ){
$hash{$matchline}++;
}
close F2;
foreach $matchline (keys %hash){
if ($hash{$matchline} == 1){
chomp($matchline);
push(#unmatched, $matchline);
}
else{
chomp($matchline);
push (#matched, $matchline);
}
}
print "Matched Entries are >>\n";
print "```````````````````````\n";
print join ("\n", #matched) . "\n";
print "```````````````````````\n";
print "Unmatched Entries are >>\n";
print "```````````````````````\n";
print join ("\n", #unmatched) . "\n";
print "```````````````````````\n";
The code you mention above will give you a false result if a given word exists more than one time in the second file and not exists in the first.
this line:
$hash{$matchline}++
increments a different counter for each different word.
in the first loop it sets to 1 for the words in the first file.
so if a word exists in each file the counter will be at least 2.
the $hash itself is a set of counters.
A more generalized version of your problem is that of computing the set union or intersection between two sets. This link gives a very good treatment of the problem in general.
In your case, the set is nothing but the list of values from each file. The logic is, if a certain value was present in both files then $hash{matchline} == 2, because the value will be incremented in both the while loops. However, if the line was present in only one of the files, the value of $hash{matchline} == 1, since only one while loop will increment the value and not the other.
Also, Lajos Veres raises a very important point: if a certain word, say "Tom" is present twice in the same file, then the algorithm will fail. It is a subtle detail, which can be resolved in many ways- removing duplicates beforehand, using two hashes, etc.
Hope this helps.

how do I get rid of single quotes around a doubly quoted string in perl? e.g ' "json_text" '

So I am having a ruby app write json response to the console which is read by another perl program that tries to convert the json response back to a perl hash. Here's my problem:
ruby app outputs the correct json output but the console adds a single quote to it like so:
my $ruby_json_out = '"{\"return\":{\"sync_enabled\":false,\"local\":true,\"name\":{\"name\":\"Sam\"}}}"'
my $ret = JSON->new->allow_nonref->decode($ruby_json_out);
Now I expect to get hash_ref in $ret but I get a string: '{"return":{"sync_enabled":false,"local":true,"name":{"name\":"Sam"}}}'.
I have searched all over the net and can't find a solution to this. When I manually stripout the single quote:
"{\"return\":{\"sync_enabled\":false,\"local\":true,\"name\":{\"name\":\"Sam\"}}}",
and run it works.
I am stuck on this for more than a day now and it's driving me crazy. I am new to perl and ruby too so I might be missing something. Any help will be appreciated greatly.
Why do you try to solve the problem on the Perl side? Woudln't it be easier to solve it on the Ruby side?
At any rate, you can use regexps to remove those doble quotes in the same way you do it manually:
my ($good_json) = ($ruby_json_out =~ /^"(.+?)"$/ ;
And then
$good_json=~ s/\\"/"/g;
Which results in
x JSON->new->allow_nonref->decode($good_json)
0 HASH(0xe4b158)
'return' => HASH(0xe4b1b8)
'local' => JSON::XS::Boolean=SCALAR(0xd22f00)
-> 1
'name' => HASH(0xe4afd8)
'name' => 'Sam'
'sync_enabled' => JSON::XS::Boolean=SCALAR(0xd22fd8)
-> 0

How do I insert a lot of whitespace in Perl?

I need to buff out a line of text with a varying but large number of whitespace. I can figure out a janky way of doing a loop and adding whitespace to $foo, then splicing that into the text, but it is not an elegant solution.
I need a little more info. Are you just appending to some text or do you need to insert it?
Either way, one easy way to get repetition is perl's 'x' operator, eg.
" " x 20000
will give you 20K spaces.
If have an existing string ($s say) and you want to pad it out to 20K, try
$s .= (" " x (20000 - length($s)))
BTW, Perl has an extensive set of operators - well worth studying if you're serious about the language.
UPDATE: The question as originally asked (it has since been edited) asked about 20K spaces, not a "lot of whitespace", hence the 20K in my answer.
If you always want the string to be a certain length you can use sprintf:
For example, to pad out $var with white space so it 20,000 characters long use:
$var = sprintf("%-20000s",$var);
use the 'x' operator:
print ' ' x 20000;