Schrift
[thread]8044[/thread]

splitten...: seeeehr einfach. (Seite 2)



<< |< 1 2 3 >| >> 21 Einträge, 3 Seiten
renee
 2006-06-01 20:57
#66892 #66892
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Code: (dl )
1
2
$line =~ s/^\s+//;
my ($wert0,$wert1,$wert2) = split(/\s+/,$line);
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
EagleFox
 2006-06-01 21:09
#66893 #66893
User since
2006-04-10
66 Artikel
BenutzerIn
[default_avatar]
läuft prima! Danke!

wo kann ich denn mal nachlesen, dass auch ich endlich mal die split parameter verstehe? Nee, falsch ausgedrückt, ich meine eher die Zeile:
Code: (dl )
$line =~ s/^\s+//;


was sagt mir das "s" vor "/"? "/^\s+" heisst doch er sucht nach mindestens einem Leerzeichen am Anfang der Zeile, oder? Und was stellt das letzte "/" an?
renee
 2006-06-01 21:45
#66894 #66894
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Die Frage zu split kann Dir perldoc -f split beantworten.

Das andere steht in perlop:

Code: (dl )
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
66
67
68
69
70
71
72
73
74
75
76
77
78
s/PATTERN/REPLACEMENT/egimosx

Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns false (specifically, the empty string).

If no string is specified via the =~ or !~ operator, the $_ variable is searched and modified. (The string specified with =~ must be scalar variable, an array element, a hash element, or an assignment to one of those, i.e., an lvalue.)

If the delimiter chosen is a single quote, no interpolation is done on either the PATTERN or the REPLACEMENT. Otherwise, if the PATTERN contains a $ that looks like a variable rather than an end-of-string test, the variable will be interpolated into the pattern at run-time. If you want the pattern compiled only once the first time the variable is interpolated, use the /o option. If the pattern evaluates to the empty string, the last successfully executed regular expression is used instead. See perlre for further explanation on these. See perllocale for discussion of additional considerations that apply when use locale is in effect.

Options are:

e Evaluate the right side as an expression.
g Replace globally, i.e., all occurrences.
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Compile pattern only once.
s Treat string as single line.
x Use extended regular expressions.

Any non-alphanumeric, non-whitespace delimiter may replace the slashes. If single quotes are used, no interpretation is done on the replacement string (the /e modifier overrides this, however). Unlike Perl 4, Perl 5 treats backticks as normal delimiters; the replacement text is not evaluated as a command. If the PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own pair of quotes, which may or may not be bracketing quotes, e.g., s(foo)(bar) or s<foo>/bar/ . A /e will cause the replacement portion to be treated as a full-fledged Perl expression and evaluated right then and there. It is, however, syntax checked at compile-time. A second e modifier will cause the replacement portion to be eval ed before being run as a Perl expression.

Examples:

s/\bgreen\b/mauve/g; # don't change wintergreen

$path =~ s|/usr/bin|/usr/local/bin|;

s/Login: $foo/Login: $bar/; # run-time pattern

($foo = $bar) =~ s/this/that/; # copy first, then change

$count = ($paragraph =~ s/Mister\b/Mr./g); # get change-count

$_ = 'abc123xyz';
s/\d+/$&*2/e; # yields 'abc246xyz'
s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz'
s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz'

s/%(.)/$percent{$1}/g; # change percent escapes; no /e
s/%(.)/$percent{$1} || $&/ge; # expr now, so /e
s/^=(\w+)/&pod($1)/ge; # use function call

# expand variables in $_, but dynamics only, using
# symbolic dereferencing
s/\$(\w+)/${$1}/g;

# Add one to the value of any numbers in the string
s/(\d+)/1 + $1/eg;

# This will expand any embedded scalar variable
# (including lexicals) in $_ : First $1 is interpolated
# to the variable name, and then evaluated
s/(\$\w+)/$1/eeg;

# Delete (most) C comments.
$program =~ s {
/\* # Match the opening delimiter.
.*? # Match a minimal number of characters.
\*/ # Match the closing delimiter.
} []gsx;

s/^\s*(.*?)\s*$/$1/; # trim white space in $_, expensively

for ($variable) { # trim white space in $variable, cheap
s/^\s+//;
s/\s+$//;
}

s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields

Note the use of $ instead of \ in the last example. Unlike sed , we use the \< digit > form in only the left hand side. Anywhere else it's $< digit >.

Occasionally, you can't use just a /g to get all the changes to occur that you might want. Here are two common cases:

# put commas in the right places in an integer
1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;

# expand tabs to 8-column spacing
1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
EagleFox
 2006-06-02 10:45
#66895 #66895
User since
2006-04-10
66 Artikel
BenutzerIn
[default_avatar]
DANKEEEEEEEEEEE!!!!!!!!!!!!

Das war genau was ich gesucht habe!
Linuxer
 2006-06-02 11:17
#66896 #66896
User since
2006-01-27
3891 Artikel
HausmeisterIn

user image
Hi,

Code (perl): (dl )
1
2
$line =~ s/^\s+//;
my ($wert0,$wert1,$wert2) = split(/\s+/,$line);


wenn man die Daten in $_ vorhaelt, kann man die Angabe der Variable weglassen. Da man an Whitespaces trennt, kann man das Pattern auch weglassen.
Wenn man das so macht, dann ignoriert split() führende Leerzeichen und man kann sich den s///-Teil sparen:

Code (perl): (dl )
1
2
# gedanklich: $_ = $line;
my ($wert0, $wert1, $wert2) = split();
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!
EagleFox
 2006-06-02 17:09
#66897 #66897
User since
2006-04-10
66 Artikel
BenutzerIn
[default_avatar]
Das geht aber wohl auch nur, wenn man genau ein Array bearbeitet. Da ich aber immer mindestens zwei verschiedene Arrays an ein sub übergebe und miteinander verarbeite fällt diese schöne Lösung wohl leider aus, wenn ich das richtig verstanden habe.
nepos
 2006-06-02 17:18
#66898 #66898
User since
2005-08-17
1420 Artikel
BenutzerIn
[Homepage] [default_avatar]
Lieber bisschen mehr hinschreiben und dann spaeter wieder durchblicken...
Crian
 2006-06-02 17:33
#66899 #66899
User since
2003-08-04
5875 Artikel
ModeratorIn
[Homepage]
user image
... und dann kann man auch noch das Klammerpaar hinter split weglassen =)

@nepos: Jein. Lieber verinnerlichen / lernen und dann kann man kürzere Ausdrücke auch selber schneller und leichter erfassen.
Das sollte natürlich nicht zum Golfen führen, aber gerade die Verwendung von $_ sollte man sich schon aneignen.\n\n

<!--EDIT|Crian|1149255327-->
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;

use strict; use warnings; Link zu meiner Perlseite
nepos
 2006-06-03 01:00
#66900 #66900
User since
2005-08-17
1420 Artikel
BenutzerIn
[Homepage] [default_avatar]
@Crian: ich weiss nicht, ich nehm da oft lieber ne extra Variable, das macht den Code auf den ersten Blick und auch nach laengerer Zeit einfacher zu verstehen. Finde ich jedenfalls.
Crian
 2006-06-07 12:52
#66901 #66901
User since
2003-08-04
5875 Artikel
ModeratorIn
[Homepage]
user image
Mir gehts andersherum =)
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;

use strict; use warnings; Link zu meiner Perlseite
<< |< 1 2 3 >| >> 21 Einträge, 3 Seiten



View all threads created 2006-06-01 00:04.