For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2006 > A string & a list









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author A string & a list
Monomachus

2006-06-30, 7:57 am




A problem in Perl

You have a string & a list
For ex.: $s = "ADbDCdBabc"; @N=("A","D","C")
You should exclude those elements from the string : first by 1 element,
after that by 2 & by 3 ... (If @N would have 5 elements
and by 4 and by 5([$#N+1] elements)), and creating a new list:

by 1:
DbDCdBabc - is excluded A
AbDCdBabc - first D
ADbCdBabc - second D
ADbDdBabc - C
(B doesn't belong to @N list)

by 2:
bDCdBabc - are excluded A,first D
DbCdBabc - A, second D
DbDdBabc - A,C
AbCdBabc - D,D
AbDdBabc - first D,C
ADbdBabc - second D,C

by 3:
bCdBabc - A,first D,second D
bDdBabc - A,first D,C
DbdBabc - A,second D,C

All these elements should be included in new list @Final...
Are there any variants how could I do smth like that ??
And one more thing could u show me variants without using RegEx




---------------------------------------------
This e-mail was sent using Mail.md



Mr. Shawn H. Corey

2006-06-30, 7:57 am

On Fri, 2006-30-06 at 12:59 +0000, Monomachus wrote:
> And one more thing could u show me variants without using RegEx


See `perldoc -f index`


--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/


Randal L. Schwartz

2006-06-30, 6:57 pm

>>>>> "Monomachus" == "Monomachus" <mono_mc@mail.md> writes:

Monomachus> A problem in Perl

Monomachus> You have a string & a list

And it looks like a homework problem.

Monomachus> And one more thing could u show me variants without using RegEx

Especially with that.

PLEASE DON'T ASK OR ANSWER HOMEWORK PROBLEMS HERE.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Rob Dixon

2006-06-30, 6:57 pm

Monomachus wrote:
>
> A problem in Perl
>
> You have a string & a list
> For ex.: $s = "ADbDCdBabc"; @N=("A","D","C")
> You should exclude those elements from the string : first by 1 element,
> after that by 2 & by 3 ... (If @N would have 5 elements
> and by 4 and by 5([$#N+1] elements)), and creating a new list:
>
> by 1:
> DbDCdBabc - is excluded A
> AbDCdBabc - first D
> ADbCdBabc - second D
> ADbDdBabc - C
> (B doesn't belong to @N list)
>
> by 2:
> bDCdBabc - are excluded A,first D
> DbCdBabc - A, second D
> DbDdBabc - A,C
> AbCdBabc - D,D
> AbDdBabc - first D,C
> ADbdBabc - second D,C
>
> by 3:
> bCdBabc - A,first D,second D
> bDdBabc - A,first D,C
> DbdBabc - A,second D,C
>
> All these elements should be included in new list @Final...
> Are there any variants how could I do smth like that ??
> And one more thing could u show me variants without using RegEx


Hi Monomachus

I hate answering queries like this with just a solution, but that's what I'm
going to do as the stuff I was going to suggest would never have led you to a
finished program. I'm also going to ignore the bit about avoiding regular
expression, because the problem is best solved using them, and if you /really/
need to take them out then its best done after a working solution is written.

I don't know how much to say about this, as I'm not sure where the sticking
points will be, but I'll point out a few bits and then be willing to answer
questions.

- The program builds a regex pattern /A|D|C/ out of the characters in the array.

- The solution is a recursive one - reduce() will remove any single occurrence
of one of the characters in the pattern from the string. Then, if more elements
must be removed, it calls itself to do the same thing to each of its results.

- The array @- is an internal one, storing the offsets of the substrings found
by the last regex pattern match. $-[0] is the offset where the whole pattern
matched, and can be used with substr() to (in this case) remove the matching
character.

- The push of the results of the recursive call is qualified by a call to grep()
to make sure the new value isn't already in the list.

- You will see that the program found a fourth case to removing three elements
from the string, changing the list to:

by 3:
bCdBabc - A,first D,second D
bDdBabc - A,first D,C
DbdBabc - A,second D,C
AbdBabc - first D,second D,C

- You say that the string should be reduced by up to $#N+1 elements, but in
general there are further solutions after this which may be relevant to your
application. For instance:

by 4:
bdBabc - A,first D,second D,C

- I've not used the results in @final, but the code puts everything in there as
you described it.

I hope I have understood your problem properly, and that this helps you. As I
said, if you are determined to do this without regexes than please let us know.
I enjoyed coding this: thanks for posting the problem.

Cheers,

Rob


** SOLUTION **

use strict;
use warnings;

my $s = "ADbDCdBabc";
my @N=("A","D","C");

my @final;

for my $depth (1 .. @N) {

printf "\nby %d:\n", $depth;

my @new = reduce($s, $depth);

print $_, "\n" foreach @new;
push @final, @new;
}


sub reduce {

my ($string, $depth) = @_;
my $pattern = join '|', @N;
my @new;

while ($string =~ /$pattern/g) {

substr my $reduction = $string, $-[0], 1, '';

if ($depth > 1) {
foreach my $value (reduce($reduction, $depth - 1)) {
push @new, $value unless grep $value eq $_, @new;
}
}
else {
push @new, $reduction;
}
}

return @new;
}

** OUTPUT **

by 1:
DbDCdBabc
AbDCdBabc
ADbCdBabc
ADbDdBabc

by 2:
bDCdBabc
DbCdBabc
DbDdBabc
AbCdBabc
AbDdBabc
ADbdBabc

by 3:
bCdBabc
bDdBabc
DbdBabc
AbdBabc
Rob Dixon

2006-06-30, 6:57 pm

(Randal L. Schwartz) wrote:
>
>
> Monomachus> A problem in Perl
>
> Monomachus> You have a string & a list
>
> And it looks like a homework problem.
>
> Monomachus> And one more thing could u show me variants without using RegEx
>
> Especially with that.
>
> PLEASE DON'T ASK OR ANSWER HOMEWORK PROBLEMS HERE.


Well maybe Randal's right, but I reckon homework counts as a Perl beginner's
problem, it's just that the form of the response should have less of a 'lets get
this thing going' slant.

Monochamus, if you're doing homework then please credit my answer to me, and
make sure you learn something as well as just getting marks in class. If not,
then let's get this thing going :)

Cheers,

Rob
DJ Stunks

2006-06-30, 6:57 pm

Monomachus wrote:
> A problem in Perl


what is?

> You have a string & a list


I have no such thing! how dare you even suggest that! that's just a
malicious rumor started by my ex girlfriend.

> You should exclude those elements from the string


I should?

> <snip>
> All these elements should be included in new list @Final...


they should?

> Are there any variants how could I do smth like that ??


huh? how could you do what like what??

> And one more thing could u show me variants without using RegEx


anything else, Your Highness?

-jp

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com