Perl: Difference between revisions
Jump to navigation
Jump to search
m →Warning |
|||
| Line 3: | Line 3: | ||
Searching and matching is explained in [[Regular Expressions]]. | Searching and matching is explained in [[Regular Expressions]]. | ||
Print out the contents of $variable in a structured format | ;Print out the contents of $variable in a structured format: | ||
<syntaxhighlight lang=Perl> | <syntaxhighlight lang=Perl> | ||
use Data::Dumper; | use Data::Dumper; | ||
| Line 18: | Line 18: | ||
<codeblock> | <codeblock> | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 41: | Line 40: | ||
;$output = `a command` | ;$output = `a command` | ||
:Execute the command and return all command output | :Execute the command and return all command output | ||
=Hashes= | |||
;%newhash = (%oldhas1,%oldhash3) | |||
:Merge 2 hashes (works is all keys are unique) | |||
==Warning== | ==Warning== | ||
If testing for existence of a hash key beware that all intermediate keys will be created by the test. In below example $hash1($ | If testing for existence of a hash key beware that all intermediate keys will be created by the test. In below example <code>$hash1($key1}</code> will be created if it does not exist yet. | ||
<syntaxhighlight lang=Perl> | <syntaxhighlight lang=Perl> | ||
if (exists $hash1{$key1}{$key2) { | if (exists $hash1{$key1}{$key2) { | ||
Revision as of 12:15, 17 December 2018
Things that do work
Searching and matching is explained in Regular Expressions.
- Print out the contents of $variable in a structured format
use Data::Dumper;
print Dumper($variable);
if-then-else:
if < expr > {
<codeblock>
} elsif < expr ) {
<codeblock>
} else {
<codeblock>
}
- $var += <value>
- Add <value> to $var (works for -, *, / too)
- $string .= $addstring
- Concatenate $string and $addstring
- $string =~ s/<regexp>/<newvalue>/[g]
- Substitute <regexp> with <newvalue> in $string. The g modifier makes the all occurrences of <regexp> are substituted.
- $newstring = substr($string,start,length)
- Return substring of $string
- sprintf(format,$string)
- Return formatted string. format is e.g. "%.3f". Check Python:Strings#Advanced for all formats.
- $exitcode = system("a command")
- Executed the command in a subprocess and return the exitcode
- $output = `a command`
- Execute the command and return all command output
Hashes
- %newhash = (%oldhas1,%oldhash3)
- Merge 2 hashes (works is all keys are unique)
Warning
If testing for existence of a hash key beware that all intermediate keys will be created by the test. In below example $hash1($key1} will be created if it does not exist yet.
if (exists $hash1{$key1}{$key2) {
...
} else {
...
}