Re: Auspaltung eines Suchmuster-String mit Perl
Tobias Erbsland <[email protected]>
| Newsgroups | gmane.linux.suse.programming |
|---|---|
| Organization | PrintSoft (Schweiz) AG |
| Message-ID | <[email protected]> |
Hallo
> wie kann ich mit Perl einen Suchmuster-String in all seine Möglichkeiten
> aufspalten?
Vermutlich gibt es das sicher irgendwo als fertiges perl Modul. Hier aber eine
einfache (nicht kryptische) Implementation.
============= snip =================
#!/usr/bin/perl -w
use strict;
# configuration
# -------------
my $pattern = '1Z23ZZ6';
my $varchar = 'Z';
my $range = '0123456789';
# 1. search all permutable chars
# ------------------------------
my %permutable;
my $pos = 0;
while( ($pos = index( $pattern, $varchar, $pos )) >= 0 )
{
$permutable{$pos} = 0;
$pos++;
}
# 2. create the permutations.
# ---------------------------
my $overflow = 0;
do
{
# display the current permutation
my $text = $pattern;
while( my ($pos, $range_pos) = each( %permutable ) )
{
substr( $text, $pos, 1, substr( $range, $range_pos, 1 ) );
}
print "$text\n";
# mutate
for my $key ( keys %permutable )
{
$permutable{$key}++;
if( $permutable{$key} >= length( $range ) )
{
$permutable{$key} = 0;
$overflow = 1;
}
else
{
$overflow = 0;
last;
}
}
}
while( ! $overflow );
=================== snip =====================
Gruss
Tobias
--
Um die Liste abzubestellen, schicken Sie eine Mail an:
[email protected]
Um eine Liste aller verfügbaren Kommandos zu bekommen, schicken
Sie eine Mail an: [email protected]