Leser: 18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl #-w
#use warnings;
#use strict;
my @keys = keys %ENV;
my @values = values %ENV;
my $counter=0;
foreach (@keys)
{
$counter++;
print "($counter) $_ ";
next unless ($_);
}
print "\n\nEnter \"all\" to display all enviroment variables. Enter name or number of variable to display single variable.\n";
my $input = (<STDIN>); # () case insensitive
$input =~ s/\s//gi; # g global, \s whitespace
if ($input =~ /(all)/i) # i case insensitive
{
my $count = @keys;
print STDOUT "$count variables to display:\n";
my $counter;
my $i=-1;
foreach (@keys)
{
$i++;
$counter = $i+1;
unless ($values[$i]) { $values[$i] = "Empty value."; }
if ($keys[$i])
{
print STDOUT "\n$counter $keys[$i]:\n$values[$i]\n";
if ($counter == $count)
{
print STDOUT "\nAll enviroment variables displayed. :)\n";
}
}
sleep(1);
}
}
else
{
if ($input =~ /[0-9]/)
{
$input = $input-1;
print "$values[$input]\n";
}
else
{
$input = uc $input;
if ($ENV{"$input"})
{
print $ENV{"$input"};
}
else
{
print "Empty value.\n";
}
}
}
QuoteDie Schnelligkeit eines Perlskripts ergibt sich aus der Abwesenheit von Modulen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl #-w
# Trial and error
#use warnings;
#use strict;
#my @keys = sort keys %ENV;
#my @values = sort values %ENV;
# connect keys and values
#print sort values %ENV;
print sort keys values %ENV;
exit;
Quote~/Dokumente$ perl env2.pl
keys on reference is experimental at env2.pl line 13.
Type of argument to keys on reference must be unblessed hashref or arrayref at env2.pl line 13.
2017-12-06T08:51:56 MuffiPerl sorgt dafür, dass die Reihenfolge von keys und values die Gleiche ist.
Von daher geht sowas schon.
1 2 3 4 5
my @vals = sort values %ENV; my @keys = sort keys %ENV; while( my($i,$e) = each @keys){ print "$e => $vals[$i]\n"; }
print map { "$_ => $ENV{$_}\n" } sort keys %ENV;
Quotelfg@lfg:~/Dokumente$ perl linus.pl
String found where operator expected at linus.pl line 4, near "say "$k => $h{$k}""
(Do you need to predeclare say?)
syntax error at linus.pl line 4, near "say "$k => $h{$k}""
Execution of linus.pl aborted due to compilation errors.