Skip to content

Instantly share code, notes, and snippets.

View jdittmarCodeSnippets's full-sized avatar

jdittmarCodeSnippets

View GitHub Profile
@jdittmarCodeSnippets
jdittmarCodeSnippets / gist:5541906
Last active December 17, 2015 03:19
JavaScript: isNumeric
// http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); }
@jdittmarCodeSnippets
jdittmarCodeSnippets / gist:5357937
Created April 10, 2013 20:00
Perl: trimErroneousCharactersAtEnds
#################################################################
# subroutine to trim off the white space, carriage returns, pipes, and commas from both ends of each string or array
sub trimErroneousCharactersAtEnds {
my $guy = shift;
return if !defined $guy;
my $type = (ref(\$guy) eq 'REF') ? ref($guy) : ref(\$guy);
if ( $type eq 'ARRAY') { # Reference to an array
foreach(@{$guy}) { #for each element in @{$guy}
s/\s+$//g;
s/,+$//g;
@jdittmarCodeSnippets
jdittmarCodeSnippets / gist:5357930
Created April 10, 2013 20:00
Perl: lineBreakCheck
# line_break_check receives a file handle as its input and returns the new line character used in the file handle
sub line_break_check{
my $file = shift;
local $/ = \1000; # read first 1000 bytes
local $_ = <$file>; # read
my ($newline) = /(\015\012?)/ ? $1 : "\012"; # Default to unix.
seek $file,0,0; # rewind to start of file
return $newline;
}
@jdittmarCodeSnippets
jdittmarCodeSnippets / gist:5357927
Created April 10, 2013 19:59
Perl: prettyPrintNumber
# formats floating point numbers so they print nicely
sub prettyPrintNumber{
my $number = shift;
if(!$number){return "0.00";}
$number = ($number < 0.001) ? sprintf('%.2e',$number) : sprintf("%.3f",$number);
return $number;
}
@jdittmarCodeSnippets
jdittmarCodeSnippets / gist:5357923
Last active May 6, 2019 15:47
Perl: isNumeric
# returns true if the value passed to it is numeric, false otherwise
sub isNumeric{
no warnings;
use warnings FATAL => 'numeric';
return 0 if(!defined $_[0] || $_[0] eq '' );
return defined eval { $_[ 0] == 0 };
}
'^[Y|y][A-Pa-p][L|R|l|r][0-9]{3}[W|w|C|c](?:-[A-Za-z])?$'
@jdittmarCodeSnippets
jdittmarCodeSnippets / gist:5330285
Created April 7, 2013 12:21
mysql: table exists
sub does_table_exist {
# check wether tables exists in the database
my ($dbHandle, $tablename) = @_;
eval { $dbHandle->do("select count(*) from $tablename where 1=0") };
if ($dbHandle->err) {
# Table does not exist
#print "Table $tablename does not exist.\n";
return 0;
}
else {