All in one IE conditional function - arrays

Trying to create and "all-in-one" IE conditional function.
Pretty much I'd like to be able to do the following:
If no variable is passed then check for IE in general
Pass it 1 version of IE and check for that
Pass an array of versions and check for either of those versions
I am having trouble with the last part which would check for multiple versions of IE. For instance, if I only wanted to target IE versions 7 and 8.
function is_ie( $versions = '' ) {
if( $versions == '' ) {
if ( isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false)
return true;
}
$versions = ( is_array($versions) ) ? $versions : array($versions);
$operand = '';
foreach( $versions as $version ) {
if ( isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ' . $version . '.' ) !== false) {
$operand = true; break;
}
}
return ($operand) ? true : false;
}
Also, is there any sacrifice on performance with this function? Saying I used it a couple times throughout a site. Wondering if I should set some type of GLOBAL variable and check for that instead?

Related

Change specific values in GSMA TAP3 file

I'm trying to change charge values from MO (originating) calls in GSM TAP3.11 files, but it doesn't handle the needed
here i can access directly duration value but with charge value its different case as attached, im tring to change the charge value on (1) chargeDetail and (2) chargeDetail (first and second records in the ChargeDetailList in the screenshot below).
My background is only with python, this's the first time with perl. I use it because after searching I believe that only perl can handle TAP files. (see TAP3::Tap3edit)
$struct=$tap3->structure;
my $key;
# Will scan all the calls for MOC's.
foreach $key ( #{$struct->{'transferBatch'}->{'callEventDetails'} } ) {
foreach ( keys %{$key} ) {
if ( $_ eq "mobileOriginatedCall" )
{
$duration= $key->{$_}->{'basicCallInformation'}->{'totalCallEventDuration'};
delete $key->{$_}{'basicCallInformation'}{'basicServiceUsedList'}[0]{'chargeInformationList'}[0]{'chargeDetailList'}[0]{'charge'};
$key->{$_}{'basicCallInformation'}{'basicServiceUsedList'}[0]{'chargeInformationList'}[0]{'chargeDetailList'}[0]{'charge'}=$duration * 0.12 /0.6;
$new_charge_value = $key->{$_}{'basicCallInformation'}{'basicServiceUsedList'}[0]{'chargeInformationList'}[0]{'chargeDetailList'}[0]{'charge'}=$duration * 0.12 /0.6;
}
}
}
$tap3->encode("$tap_file") or die $tap3->error;
The exists() function in Perl is used to check whether an element in an given array or hash exists or not. This function returns 1 if the desired element is present in the given array or hash else returns 0.
$Tax_Rate = 3;
$Exchange_Rate = 3;
$Rate_Plan_Charge_Rate = 8;
my $key;
# Will scan all the calls for MOC's.
foreach $key ( #{$struct->{'transferBatch'}->{'callEventDetails'} } ) {
foreach ( keys %{$key} ) {
if ( $_ eq "mobileOriginatedCall" )
{
if (exists $key->{$_}->{'basicCallInformation'}->{'totalCallEventDuration'}){
$duration = $key->{$_}->{'basicCallInformation'}->{'totalCallEventDuration'};
if (exists $key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{chargeDetailList}[0]){
$key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{chargeDetailList}[0]{charge}=($duration * $Rate_Plan_Charge_Rate) / $Exchange_Rate;
}
if (exists $key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{chargeDetailList}[1]){
$key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{chargeDetailList}[1]{charge}=($duration * $Rate_Plan_Charge_Rate) / $Exchange_Rate;
}
$New_Charge = $key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{chargeDetailList}[0]{charge};
if (exists $key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{taxInformation}[0]){
$key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{taxInformation}[0]{taxValue}=($New_Charge / $Tax_Rate);
}
if (exists $key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{taxInformation}[1]){
$key->{$_}->{basicServiceUsedList}[0]{chargeInformationList}[0]{taxInformation}[0]{taxValue}=($New_Charge / $Tax_Rate);
}
}
}
}
}

Reduce the number of conditional operators (5) used in the expression (maximum allowed 3)

I have the following condition in my angular application
else if (((column.field === "ttUser" 1|| column.field === "ttAdmin") 2&& $scope.EngttAccess) 3|| ((column.field === "btUser" 4|| column.field === "btAdmin") 5&& $scope.EngbtAccess)) {
how can i make this to solve sonar qube issue?
This should behave same way as your example.
else if (
(["ttUser", "ttAdmin"].includes(column.field) && $scope.EngttAccess) ||
(["btUser", "btAdmin"].includes(column.field) && $scope.EngbtAccess)) {

Use of uninitialized value $end_time1 in string eq .......error in perl

i want to retrive some data from my database,if $end_time1 is null then will be update table ,otherwise will do nothing ,but when i run the code,i found the $end_time1 is null,then will be update the table ,but if not null ,it's will be return the error :
Use of uninitialized value $end_time1 in string eq ........
part of my code :
my $select_sth = $dbh->prepare("SELECT id ,H1, H2, addr1, addr2, time_1, time_2,
end_time_1,end_time_2,count1,count2 FROM service") or die "$dbh->errstr";
$select_sth->execute() or die "$dbh->errstr";
while (my #row_ref = $select_sth->fetchrow_array)
{
my $Rid = $row_ref[0];
my $addr1 = $row_ref[3];
my $addr2 = $row_ref[4];
my $end_time1 = "NULL" unless $row_ref[7];
my $end_time2 = "NULL" unless $row_ref[8];
my $count1 = $row_ref[9];
my $count2 = $row_ref[10];
if($end_time1 eq "NULL")
{
print "$end_time1 is null\n";
$dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
}
}
please someone what's wrong with my code ?how to fix ?
Your current code only sets $endtime1 if $row_ref[7] is not defined. This means that $endtime1 is undefined if $row_ref[7] does have a value, so you get that Use of uninitialized value $end_time1 in string eq... error when you test it.
Change your code so that $endtime1 will either be set to $row_ref[7] (if it is defined) or to NULL:
my $end_time1 = $row_ref[7] || 'NULL';
Then you can use your existing code:
if ($end_time1 eq "NULL")
{
print "end_time1 is null\n";
$dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
}
The same issue exists for $endtime2, so you may want to make a similar alteration.
Use defined:
if ( !defined $end_time1 ) {
print "end_time1 is null\n";
}

Array indexing in Confluence / Velocity templates

Our company has begun using Confluence wiki as a documentation site for our teams, and we are implementing a macro called PocketQuery.
This macro pulls SQL data into the site dynamically (we are pulling information like site contacts).
It uses Velocity Templates to display it's data, however I am having issues when doing simple array indexing.
The code:
#set ( $page = $additionalParams.get('Page') )
#set ( $pages = "" )
#if ( $page != $null && $page != "" )
#set ( $pages = $page.split(";") )
#else
#set ( $pages = [] )
#end
The $additionalParams is a list that has been initialised outside of the template, and contains the parameters being passed into macro, in this case:
Page=Site Name;Server
The code I am trying to setup is pulling the Name;Server value from the $additionalParams list, split the value if it is not empty, and then obtain the first value.
I have tried:
$pages.get(1)
$pages[1]
However no value gets pulled (I have also tried zero as an index - same result).
Foreach-ing through this array and printing each entry does work - meaning there are values in there.
All I want to be able to do is index into the array - Can't seem to dig up anyway of doing this.
Could this be converted to a list so it can use the $pages.get method to index into it?
Extending on that would this allow me to use $pages.contains method?
Confluence is using Velocity 1.6.
EDIT:
The solutions on this page and on this page do not work - I am guessing (Wildly) maybe the correct objects are not in the context for the template to use them?
(Would PocketQuery or Confluence be doing this?)
How would I go about using the 'ListTool' ?
The only way I could get this to work with any kind of elegance is as follows:
#set ( $Page = $additionalParams.get('Page') )
#set ( $Pages = [] )
#if ( $Page != $null && $Page != "" )
#foreach($i in $Page.split(";"))
$Pages.add($i)
#end
#end
This initialises a seperate array, loops through the split values and adds them to the seperate array, which seems to be able to use the methods provided in this question
I could then proceed to use it thus:
$Pages.get(0)//Would return "Site Name"
And also
$Pages.contains("Site Name")//Would return true
m.t.bennet is actually right - I tested and it works!!! I took about a month to find the asnwer and his code sniplet works
#set ( $Page = $additionalParams.get('Page') )
#set ( $Pages = [] )
#if ( $Page != $null && $Page != "" )
#foreach($i in $Page.split(";"))
$Pages.add($i)
#end
#end

Evaluating equality in perl using elements taken from an array ref

I have a small perl script that needs to evaluate the equality of two parameters and a small return from the database.
my ($firstId, $secondId, $firstReturnedId, $secondReturnedId, $picCount);
my $pics = $dbh->prepare(qq[select id from pictures limit 10]);
$firstId = q->param('firstId');
$secondId = q->param('secondId');
$pics->execute or die;
my $picids = $pics->fetchall_arrayref;
$picCount = scalar(#{$picids});
$firstReturnedId = $picCount > 0 ? shift(#{$picids}) : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids}) : $firstReturnedId;
Here, a quick look at my debugger shows that $picCount = 1 and $firstReturnedId = 9020 and $secondReturnedId = 9020. However, they are both denoted as
ARRAY(0x9e79184)
0 9020
in the debugger so when I perform the final check
my $result = (($firstId == $firstReturnedId) && ($secondId == $secondReturnedId)) ? 1 : 0;
I get $result = 0, which is not what I want.
What am I doing wrong?
DBI::fetchall_arrayref returns a reference to a list of "row results". But since there could be more than one value in a row result (e.g., your query could have been select id,other_field from pictures), each row result is also a reference to a list. This means you have one more dereferencing to do in order to get the result you want. Try:
$picCount = scalar(#{$picids});
if ($picCount > 0) {
my $result = shift #{$picids};
$firstReturnedId = $result->[0];
} else {
$firstReturnedId = 0;
}
if ($picCount > 1) {
my $result = pop #{$picids};
$secondReturnedId = $result->[0];
} else {
$secondReturnedId = $firstReturnedId;
}
or if you still want to use a concise style:
$firstReturnedId = $picCount > 0 ? shift(#{$picids})->[0] : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids})->[0] : $firstReturnedId;

Resources