Last active
April 11, 2018 17:18
-
-
Save xremix/a0212eddc4d1bba223d68c7a4aa8e36a to your computer and use it in GitHub Desktop.
Perl cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# ^ wird verwendet wenn mehrere Perl installiert | |
use Math::Trig; # Mathe Library | |
# spitze klammern sind Handle | |
chomp($r = <STDIN>); # Zeile einlesen | |
# chomp Entfernt alle strings die noch aus der vorherigen eingeabe vorhanden sind | |
print 2 * pi * $r; | |
# --------- Listen ----------- | |
@buch = (1,2,3); | |
$buch[0] = "1"; | |
print @buch; | |
# ABER! | |
print $buch[0]; | |
#Können beide existiereten | |
@a = (); | |
$a = "a"; | |
("Peter", "Hans") == qe( Peter Hans ); | |
# Weitere zeichen für funktionen z.B. | |
print ("a"); | |
print !"a"!; | |
print {"a"}; | |
echo ! mit ausrufe\!zeichen !; | |
# Liste kopieren: | |
@listeb = ("a", 2); | |
@listea = @listeb; | |
# Anzahl der elemente | |
0 + @personen # ergibt anzahl | |
sub count_arguments{ | |
$a = @_; | |
} | |
sub count_arguments2{ | |
0 + @_; | |
} | |
sub count_arguments_manu{ | |
scalar(@_); | |
} | |
sub count_arguments3{ | |
$i = -1; | |
for(0..@_){ | |
$i++; | |
} | |
$i; | |
} | |
sub count_arguments4{ | |
$#_ + 1; | |
} | |
# stack | |
push @listeb "neu"; | |
push @listeb ("noch", "mehr"); | |
pop @listeb; | |
#queue | |
shift @kram; | |
unshift @kram "Stift"; | |
# sort reverse | |
@kram = sort @kram; | |
@kram = reverse @kram; | |
# ------------ Variablen tauschen ------- | |
($links, $rechts) = ($rechts, $links); | |
# Foreach | |
foreach $item (@kram){ | |
} | |
foreach (1..10){ | |
print "Durchlauf $_"; | |
} | |
# ------------ Sub ------- | |
sub x{ | |
print($#_); | |
print("\n"); | |
print(@_ + 0); | |
} | |
&x("", "", ""); | |
# ------------ Filehandle ------- | |
open DATEI, "datei.txt"; | |
# < Lesend, >schreiend, >>anhängend | |
open DATEI, "<datei.txt"; | |
$zeile = <DATEI>; | |
while (defined($zeile = <DATEI>)); | |
while (<DATEI>){} | |
@zeilen = <DATEI>; | |
close DATEI; | |
$success = open DATEI, "datei.txt"; | |
die "fehler $!"; | |
print DATEI "Inhalt"; | |
# Zeilenweise einlesen | |
open DATEI, "datei.txt"; | |
while(<DATEI>){ | |
chomp($_); | |
} | |
close DATEI; | |
# --------Hash---------- | |
%hash = (a=>1, | |
b=>2); | |
$var = $hash{'a'}; | |
@arr = $hash; | |
$hash = @arr; | |
# loop | |
foreach my $key (sort(keys(%hash))){ | |
print "wert ist".$hash{$key}; | |
} | |
each (%hash); // gibt lsite aller paare zurück | |
while($schlüssel, $wert) = each %hash){ | |
} | |
exists ($hash{"value"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment