App-Cheats

 view release on metacpan or  search on metacpan

cheats.txt  view on Meta::CPAN

#
# Or this way
#include <map>
if (map.find(item) != map.end())
   do_this();
else
   do_that();

# Cpp string to std::vector function
std::vector<std::string> stringToVector(std::string myString)
{
	std::vector<std::string> myVec;
	std::istringstream ss(myString);
	std::string token;
	while (std::getline(ss, token, ','))
        myVec.push_back(token);
	return myVec;
}

# Cpp working with vectors and tuples (arrays)
std::vector<std::tuple<std::string, std::string, std::string>> possibilities_vec;
possibilities_vec.push_back(std::make_tuple( row.get_string(0), row.get_string(1), row.get_string(2) ));
body += "\n\n// options:";
for (auto &val : possibilities_vec)
	body += "\n//   " + std::get<0>(row) + " " + std::get<1>(row) + " " + std::get<2>(row);

# Check if a vector is empty (cpp)
if (Vector.empty()) { /* operations */ }

# Cpp int to string (toString)
to_string(USE_DISPLAY_NONE)

# Cpp check if a value is defined (not null)
std::string Renderer::renderTable(const std::string &module, const std::string *id /*= nullptr*/)
if (id != nullptr)
//
// declared here
std::string renderTable(const std::string &module, const std::string *id = nullptr,
	const std::string *useDisplayNone = nullptr, const std::string *useTablePush = nullptr); // TODO: added for debug


#############################################################
## CKEditor
#############################################################

# Make the CKEditor to be readonly.
CKEDITOR.instances.editor1.setReadOnly(true)


#############################################################
## Cmake
#############################################################

# Exit from cmake
return()

# Print a message in cmake
message( "PROJECT_NAME: ${PROJECT_NAME}" )

# Assign variable in cmake
set(GENERATE_OUTPUT_DIR "${CMAKE_BINARY_DIR}/src_generated/")

# Remove files from a folder using cmake
file(GLOB REMOVE_FILES "${CMAKE_BINARY_DIR}/*")
message("REMOVE_FILES: ${REMOVE_FILES}")
file(REMOVE_RECURSE ${REMOVE_FILES})

# Include submodules in *.cmake
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/tools/cmake")
include(macros_internal)
include(macros_public)

# Minimal cmake example (step0)
# Copy into CMakeLists.txt
#
cmake_minimum_required(VERSION 3.10)
#
# set the project name
project(Tutorial VERSION 1.0)
#
# After assigned project, PROJECT_SOURCE_DIR is set
message("")
message("PROJECT_NAME:       ${PROJECT_NAME}")
message("PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
message("PROJECT_BINARY_DIR: ${PROJECT_BINARY_DIR}")
message("")
#
# Add an executable to the project using the specified source files.
add_executable(Tutorial tutorial.cxx)

# Minimal cmake example (step0)
# Copy into tutorial.cxx
#
#include <iostream>
int main(int argc, char* argv[])
{
    std::cout << "Hello CMake" << std::endl;
    return 0;
}

# Building with cmake (step0)
mkdir build
cd build
cmake ..
cmake --build .


#############################################################
## Chrome
#############################################################

# Jump to address bar in Chrome (url,pi)
Control + L

# Toggle Chrome browser navigation bar (favorites)
Control + Shift + B

# View source code in Android chrome browser
view-source:MY_URL

# Audit the speed of a website using the Google Chrome extension Lighthouse.

cheats.txt  view on Meta::CPAN

    sub f { say $^S }
    f;
    eval { f };
    eval "f";
'
0
1
1


#############################################################
## Perl File Test Markers
#############################################################

# Example of reading from the end of file in perl.
while (<DATA>){
    $r = /start/../end/;
    print if $r > 1 and $r !~ /E0$/;
}
__DATA__
junk1
start
data1
data2
data4
end
junk2

# On systems where $0 cannot be used to find out
# the file size, one can maybe still use <DATA>
# to determine the size of the file.
use POSIX qw(strftime);
$raw_time = (stat(DATA))[9];
$size     = -s DATA;
$kilosize = int($size / 1024) . "k";
print "<P>Script size is $kilosize\n";
print strftime(
"<P>Last script update: %c (%Z)\n", localtime($raw_time)
);
__DATA__
DO NOT REMOVE THE PRECEDING LINE.


#############################################################
## Perl File Syntax
#############################################################

# Can use "#" as a line number directives. (Perl File Syntax)
# https://perldoc.perl.org/perlsyn#Plain-Old-Comments-(Not!)
# Note: It marks the NEXT line.
perl -E 'eval qq(# line 123 myfile.txt\ndie "My bad"); say $@'
My bad at myfile.txt line 123.

# Perl File Syntax
# When  opened  for  reading,  the special
# filename  “–”  refers  to  STDIN.  When
# opened for  writing,  the  same  special
# filename  refers  to  STDOUT.
# Normally,  these  are  specified as “<–” and “>–”,
# respectively.
open(INPUT,  "–" ) || die;     # re–open standard input for reading
open(INPUT,  "<–") || die;     # same thing, but explicit
open(OUTPUT, ">–") || die;     # re–open standard output for writing

# Can always use / for files in perl (even on DOS).
my $path = "a\b\c.txt";
my $path = "a/b/c.txt";

# Read piped output in perl.
open PIPE, "-|", "perl out.pl" or die $!;
while( my $line = <PIPE> ){
	print $line;
}
close PIPE;


#############################################################
## Perl File Test Operators
#############################################################

# Check if reading from pipe or standard input STDIN keyboard
echo "abc" | perl -le 'print -t STDIN ? "STDIN" : "pipe"' 	# "pipe"
perl -le 'print -t STDIN ? "STDIN" : "pipe"' 			# "STDIN"

# Read from either PIPE or from standard input STDIN (in Perl)
perl -le 'push @ARGV, <STDIN> unless -t STDIN; print "[$_]" for @ARGV'
perl -le 'push @ARGV, <STDIN> unless -t; print "[$_]" for @ARGV'
perl -le 'push @ARGV, map /\S+/g,<STDIN> unless -t; print "[$_]" for @ARGV'
perl -le 'print -t() ? "RIGHT" : "LEFT"'
echo | perl -le 'print -t() ? "RIGHT" : "LEFT"'


#############################################################
## Perl Golf - General
#############################################################

# Get the Path variable on DOS
path | perl -E "$/=';'; say for <>"
path | perl -073 -nE "say"
path | perl -073 -l12 -pe ""
path | perl -073l12 -pe ""
path | perl -073l12pe ""
path | perl -073l12pe0
path | perl -lp073e0

# Get the Path variable on Linux
path | perl -073 -nE "say"
echo "$PATH" | perl -E '$/=":"; say for <>'
echo "$PATH" | perl -072 -nE 'say'
echo "$PATH" | perl -072 -l12 -pe ''
echo "$PATH" | perl -072l12 -pe ''
echo "$PATH" | perl -072l12pe ''
echo "$PATH" | perl -072l12pe0
echo "$PATH" | perl -lp072e0


#############################################################
## Perl Golf - Column Selection
#############################################################

# Get Nth column of data (-e must be last)
ll * | perl -ane 'print "$F[8]\n"'

cheats.txt  view on Meta::CPAN

[1]
1
[2]
2
[3]
3


#############################################################
## Perl Functions - Recursion
#############################################################

# Naturally recursive function using queue technique.
sub run_per_scalar {
    my ( $data, $code ) = @_;
    my @queue = ( $data );
    my %seen;
    while ( my $item = shift @queue ) {
        next if $seen{$item}++;
        my $ref = ref $item;
        if ( $ref eq "ARRAY" ) {
            unshift @queue, map { ref $_ ? $_ : \$_ } @$item;
        }
        elsif ( $ref eq "HASH" ) {
            unshift @queue, map { ref $_ ? $_ : \$_ } values %$item;
        }
        elsif ( $ref eq 'SCALAR' ) {
            $code->() for $item;
        }
        elsif( !$ref ){
            die "Not a reference!\n";
        }
        else {
            die "Not supported reference type: $ref!\n";
        }
    }
}

#############################################################
## Perl Functions - flock
#############################################################

# Simple example of flock.
# Wait indefinitely for a lock.
perl -E 'use Fcntl ":flock"; open $fh, "+>>", "my.txt" or die $!; flock $fh, LOCK_EX or die $!; say $fh 111; sleep 10'&
perl -E 'use Fcntl ":flock"; open $fh, "+>>", "my.txt" or die $!; flock $fh, LOCK_EX or die $!; say $fh 222'

# Flock perl explanation/guide.
https://www.perlmonks.org/?node_id=7058
https://perl.plover.com/yak/flock/


#############################################################
## Perl Functions - fork
#############################################################

# Run a child in a separate process and wait for it.
perl -E 'my $PID = fork; if (!$PID){ sleep 2; say "child"; exit 0 } waitpid $PID, 0; say "parent"'

# Send data from forked child back to parent using pipes.
perl -E 'pipe(INPUT,OUTPUT); my $PID = fork; if (!$PID){ close INPUT; sleep 1; say "child"; say OUTPUT "42"; close OUTPUT; exit 0 } close OUTPUT; waitpid $PID, 0; say "parent"; my ($Data) = <INPUT>; say "[$Data]"'

# Run multiple processes and collect their data.
perl -MMojo::Util=dumper -E 'use strict; use warnings; local $| = 1; my @Wait; for my $Count (1..5){ my($In,$Out); pipe($In,$Out); my $PID = fork; if (!$PID){ close $In; sleep int(rand(5)); say "Running child $Count"; say $Out "From-$Count"; close $O...
Running child 2
Running child 3
Running child 4
Running child 5
Running child 1
parent is reading now pid: 593943
parent is reading now pid: 593944
parent is reading now pid: 593945
parent is reading now pid: 593946
parent is reading now pid: 593947
{
  "593943" => [
    "From-1"
  ],
  "593944" => [
    "From-2"
  ],
  "593945" => [
    "From-3"
  ],
  "593946" => [
    "From-4"
  ],
  "593947" => [
    "From-5"
  ]
}

# Run multiple processes and collect their data (no debug output).
perl -MMojo::Util=dumper -E 'use strict; use warnings; local $| = 1; my @Wait; for my $Count (1..200){ my($In,$Out); pipe($In,$Out); my $PID = fork; if (!$PID){ close $In; sleep int(rand(5)); say $Out "From-$Count"; close $Out; exit 0 } close $Out; p...

# Run multiple processes and collect their data.
# Sends a structure back from the children.
perl -Mojo -E 'local $| = 1; my @Wait; for my $Count (1..5){ my($In,$Out); pipe($In,$Out); my $PID = fork; if (!$PID){ close $In; sleep int(rand(5)); say $Out j { Title => "From-$Count", Count => $Count }; close $Out; exit 0 } close $Out; push @Wait,...
{
  "1" => {
    "ChildPid" => 28843,
    "Count" => 1,
    "Title" => "From-1"
  },
  "2" => {
    "ChildPid" => 28844,
    "Count" => 2,
    "Title" => "From-2"
  },
  "3" => {
    "ChildPid" => 28845,
    "Count" => 3,
    "Title" => "From-3"
  },
  "4" => {
    "ChildPid" => 28846,
    "Count" => 4,
    "Title" => "From-4"
  },
  "5" => {
    "ChildPid" => 28847,



( run in 1.639 second using v1.01-cache-2.11-cpan-13bb782fe5a )