Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]6399[/thread]

substr_count in perl



<< |< 1 2 3 >| >> 30 Einträge, 3 Seiten
norman
 2004-07-14 14:52
#84288 #84288
User since
2004-03-11
46 Artikel
BenutzerIn
[default_avatar]
hallo mal wieder,

gibts in perl etwas äquivalentes zur funktion "substr_count" (z.b.: zähle das vorkommen vom buchstaben "a" im string "abba" -> ergebnis 2) in php?

gruß,
norman
betterworld
 2004-07-14 15:02
#84289 #84289
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
Ja.
Code: (dl )
1
2
3
4
5
my $string = "abba";
my $substr_count = $string =~ y/a/a/;

#oder auch:
my $substr_count_2 = @{[$string =~ m/(a)/g)]};

Die erste Form funktioniert nur, wenn "a" nur ein einziger Buchstabe ist.\n\n

<!--EDIT|betterworld|1089803224-->
pq
 2004-07-14 15:03
#84290 #84290
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
buchstaben zählen:
my $count = $string =~ tr/a//;
strings zählen:
my $match = "gesucht";
my $count = () = $string =~ m/\Q$match/g;
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
pq
 2004-07-14 15:05
#84291 #84291
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
[quote=betterworld,14.07.2004, 13:02]
Code: (dl )
my $substr_count_2 = scalar($string =~ m/(a)/g);

Die erste Form funktioniert nur, wenn "a" nur ein einziger Buchstabe ist.[/quote]
die zweite gar nicht
SCNR =)
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
betterworld
 2004-07-14 15:08
#84292 #84292
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
[quote=pq,14.07.2004, 13:05][quote=betterworld,14.07.2004, 13:02]
Code: (dl )
my $substr_count_2 = @{[$string =~ m/(a)/g]};

Die erste Form funktioniert nur, wenn "a" nur ein einziger Buchstabe ist.[/quote]
die zweite gar nicht
SCNR =)[/quote]
Ich weiss nicht ganz, wie Du zu dieser Annahme kommst ;-) *schummel*
betterworld
 2004-07-14 15:11
#84293 #84293
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
[quote=pq,14.07.2004, 13:03]my $count = () = $string =~ m/\Q$match/g;[/quote]
Das ist aber auch kein uncooles Konstrukt. Duerfte ich erfahren, warum das funktioniert?\n\n

<!--EDIT|betterworld|1089803521-->
pq
 2004-07-14 15:13
#84294 #84294
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
[quote=betterworld,14.07.2004, 13:08]Ich weiss nicht ganz, wie Du zu dieser Annahme kommst ;-) *schummel*[/quote]
nanana, hier wird nicht geschummelt! =)
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
pq
 2004-07-14 15:17
#84295 #84295
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
[quote=betterworld,14.07.2004, 13:11][quote=pq,14.07.2004, 13:03]my $count = () = $string =~ m/\Q$match/g;[/quote]
Das ist aber auch kein uncooles Konstrukt. Duerfte ich erfahren, warum das funktioniert?[/quote]
genau erklären kann ich's auch nicht. =~ wird auf jeden fall im
listenkontext ausgeführt und gibt deswegen alle matches zurück, also
eine liste. diese wird an () zugewisen. nun wird das ergebnis dieser
zuweisung an einen skalar zugewiesen.
da das ergebnis aber eine liste (und kein array) war, frage ich mich,
warum die anzahl herauskommt und nicht das letzte element.
ich wusste das aber mal... hm...
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
Taulmarill
 2004-07-14 17:05
#84296 #84296
User since
2004-02-19
1750 Artikel
BenutzerIn

user image
hab (mit nen bischen hilfe von sri) was im perldoc gefunden

Code: (dl )
$count = () = $string =~ /\d+/g;

will place into $count the number of digit groups found in $string. This happens because the pattern match is in list context (since it is being assigned to the empty list), and will therefore return a list of all matching parts of the string. The list assignment in scalar context will translate that into the number of elements (here, the number of times the pattern matched) and assign that to $count. Note that simply using
$_=unpack"B*",~pack"H*",$_ and y&1|0& |#&&print"$_\n"for@.=qw BFA2F7C39139F45F78
0A28104594444504400 0A2F107D54447DE7800 0A2110453444450500 73CF1045138445F4800 0
F3EF2044E3D17DE 8A08A0451412411 F3CF207DF41C79E 820A20451412414 83E93C4513D17D2B
betterworld
 2004-07-14 17:45
#84297 #84297
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
[quote=Taulmarill,14.07.2004, 15:05]The list assignment in scalar context will translate that into the number of elements[/quote]
Das sollte doch aber eigentlich nicht bei einem List-assignment sondern nur bei array-assignments passieren. Bei ersterem wird normalerweise das letzte Element zurueckgegeben, wie pq schon schreibt. Beispiel:
Code: (dl )
1
2
my $x=("warner","brosers");
print $x; # warner
<< |< 1 2 3 >| >> 30 Einträge, 3 Seiten



View all threads created 2004-07-14 14:52.