find similar characters in strings?

J

jonas.huckestein

hi guys,

i've got the following problem: i have lots of strings with only 1 and
0 as characters. all are of the same length. now i need to create a
string which has a 1 where all of the strings have a 1 and a 0
everywhere else.

thanks in advance, you're great
 
P

Paul Lalli

i've got the following problem: i have lots of strings with only 1 and
0 as characters. all are of the same length. now i need to create a
string which has a 1 where all of the strings have a 1 and a 0
everywhere else.

Have a look at
perldoc perlop

Specifically, the section on the & bit-wise operator.

Then make an attempt, and if it doesn't work, feel free to post your
attempt here for assistance in debugging it.

Paul Lalli
 
G

Gunnar Hjalmarsson

i've got the following problem: i have lots of strings with only 1 and
0 as characters. all are of the same length. now i need to create a
string which has a 1 where all of the strings have a 1 and a 0
everywhere else.

Not sure what it is you need, but if you are going to use Perl for the
task, it sounds as if the tr/// operator might be useful. Look up the
docs for that operator in

perldoc perlop
 
P

Paul Lalli

Gunnar said:
Not sure what it is you need, but if you are going to use Perl for the
task, it sounds as if the tr/// operator might be useful. Look up the
docs for that operator in

perldoc perlop

I agree that the OP was not at all clear in his description, but I
think he's looking to AND-together a sequence of strings, ala:
11010101
01101111
11011011
--------
01000001

If that's the case, I'm not clear on how tr/// would help here.

Of course, I could be wrong. It would have been nice if the OP had
posted some sample input and desired output to clarify his desires.
Gee, if only there were some sort of document that helped people learn
how to create a useful post. A sort of "guidelines", if you will...

Paul Lalli
 
G

Gunnar Hjalmarsson

Paul said:
I agree that the OP was not at all clear in his description, but I
think he's looking to AND-together a sequence of strings, ala:
11010101
01101111
11011011

Me neither.
Of course, I could be wrong.

Your interpretation does seem to fit the OP. Thanks for your possible
clarification. ;-)
It would have been nice if the OP had
posted some sample input and desired output to clarify his desires.
Gee, if only there were some sort of document that helped people learn
how to create a useful post. A sort of "guidelines", if you will...

Yeah, such a document would have been nice...

Actually, it's amazing how often programmers, who inevitably have the
ability of structured thinking, post vague and/or incomplete queries
about programming problems. It shows disrespect, whether they are aware
of the posting guidelines or not. :(
 
B

Babacio

Gunnar Hjalmarsson.
Actually, it's amazing how often programmers, who inevitably have the
ability of structured thinking, post vague and/or incomplete queries
about programming problems. It shows disrespect, whether they are
aware of the posting guidelines or not. :(

Or it may show that they don't have so much hability of structured
thinking. I'm not speaking of the present case, no offense is meant,
but sometimes, that's the only rational solution.
 
G

Gunnar Hjalmarsson

Dr.Ruud said:
Paul Lalli schreef:

I like to refer to "How to Ask Questions the Smart Way"

http://www.catb.org/~esr/faqs/smart-questions.html

but that might be broader than what you had in mind.

Yes and no.

Paul's comment was ironic. The guidelines he'd like to see are posted
here twice a week, and they are also available on the web:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

That link appears first if you search for

usenet perl guidelines

at Google.

Oh, and the smart-questions link you mentioned is included at the top of
the clpmisc guidelines.
 
J

jonas.huckestein

ok guys,

thanks for the help ... in fact, i was quite sure, that my query was
not as vague as some of you appear to have understood it. i am also
aware, that it's not really helpful to criticize the people you're
asking for help, anyway i'd like you to reconsider discussing some ot
about posting guidelines (which i have read --- before i posted my
first msg) in this thread ...

Paul Lalli got it all right, i want to basically binary-add up
fixed-length ascii-strings which contain '0' and '1', character-wise
(10 + 11 = 10).

Anyway, my need has changed a bit (in fact the adding technique didn't
work ... *dough*). I need to have a 1 in the output string, if _the
majority of remaining strings_ (more than half) has a 1 there. My
attempt is to simply iterate through the indices $i of the strings and
then add up (arithmetically) all $i'th characters in all strings. If
they amount to more than half of the number of strings, i'll write a 1.
I didn't implement it yet (not working right now).

Just to clarify: I am indeed capable of writing such trivial snippets
myself. Yet in my (very recent) experience with perl i have learned,
that there are A LOT of very neat and short ways of doing such things.
Additionally If your project is supposed to scale properly you also
need to consider the speed of your implementation of such frequently
used code-snippets ... I think it's fun to think of such snippets which
are small, fast and do a lot, that's why I posted ...
 
P

Paul Lalli


To which guys are you speaking? Please quote some relevant context
when you post a reply. Have you seen the Posting Guidelines?
thanks for the help ... in fact, i was quite sure, that my query was
not as vague as some of you appear to have understood it.

That's precisely how we know that it *was* vague - that only *some*
people understood what you wanted. If what you posted had not been
vague, everyone would have understood it, and your chances for a
helpful response would have increased significantly.

Have you read the Posting Guidelines?
i am also
aware, that it's not really helpful to criticize the people you're
asking for help, anyway i'd like you to reconsider discussing some ot
about posting guidelines (which i have read --- before i posted my
first msg) in this thread ...

Ahhh, so you have read them. But you choose not to follow them.
Explain to us, then, why any of us should make an attempt to help you
if you can't be bothered to follow the simple requests contained
therein?

Good bye.
Paul Lalli
 
B

Babacio

jonas.huckestein.
ok guys,

thanks for the help ... in fact, i was quite sure, that my query was
not as vague as some of you appear to have understood it.

Well, I did not try to answer, but when I read it I first
misunderstood it. I had to read Paul Lalli's answer to get it.

Put maybe the problem is with Gunnar and myself, we are not clever
enough, that's it.
 
A

Anno Siegel

Anyway, my need has changed a bit
Lovely.

(in fact the adding technique didn't work ... *dough*).

....nuts?

In which way didn't it work? It does for me.
I need to have a 1 in the output string, if _the
majority of remaining strings_ (more than half) has a 1 there.

use List::Util qw( sum);
my @l = qw( 100000 110000 111000 111100 111110 111111);

my $result = reverse join '',
map sprintf( '%.0f', sum( map chop, @l)/@l), 1 .. length $l[ 0];

Anno
 
X

xhoster

ok guys,

thanks for the help ... in fact, i was quite sure, that my query was
not as vague as some of you appear to have understood it.

I am quite sure that it was as vague as some of us understood it.
i am also
aware, that it's not really helpful to criticize the people you're
asking for help, anyway i'd like you to reconsider discussing some ot
about posting guidelines (which i have read --- before i posted my
first msg) in this thread ...

If you read them, then why didn't you follow them?

Paul Lalli got it all right, i want to basically binary-add up
fixed-length ascii-strings which contain '0' and '1', character-wise
(10 + 11 = 10).

What you want is not "add"ing up, that is "and"ing up. And 10+11 does not
give 10, at least not in Perl. See, we want you to post Perl, not some
psuedo language and not only some vague English, to avoid exactly these
problems.

Anyway, my need has changed a bit (in fact the adding technique didn't
work ... *dough*). I need to have a 1 in the output string, if _the
majority of remaining strings_ (more than half) has a 1 there. My
attempt is to simply iterate through the indices $i of the strings and
then add up (arithmetically) all $i'th characters in all strings. If
they amount to more than half of the number of strings, i'll write a 1.
I didn't implement it yet (not working right now).

I'd just split the strings into arrays and work with them that way.
my @x = split //, $string;

Just to clarify: I am indeed capable of writing such trivial snippets
myself.

Then why didn't you?
Yet in my (very recent) experience with perl i have learned,
that there are A LOT of very neat and short ways of doing such things.

If you give us short, runnable code that does what you want, we can help
you turn it into code that is neater, faster, and/or even shorter. If you
give us vague English desciptions of what you want, we are much less likely
to be able to do that.

Additionally If your project is supposed to scale properly you also
need to consider the speed of your implementation of such frequently
used code-snippets ... I think it's fun to think of such snippets which
are small, fast and do a lot, that's why I posted ...

If you include the slow snippet, we will know exactly what you want done,
we will know that you actually put some effort into it, we will be able to
test our proposed code to make sure it does the same thing as your slow
snippet, and we won't waste our time making something which is essentially
identical to what you already have.

Xho
 
G

Gunnar Hjalmarsson

If you include the slow snippet, we will know exactly what you want done,
we will know that you actually put some effort into it, we will be able to
test our proposed code to make sure it does the same thing as your slow
snippet, and we won't waste our time making something which is essentially
identical to what you already have.

Or, as the guidelines put it: "Speak Perl rather than English, when
possible".
 
W

William James

ok guys,

thanks for the help ... in fact, i was quite sure, that my query was
not as vague as some of you appear to have understood it. i am also
aware, that it's not really helpful to criticize the people you're
asking for help, anyway i'd like you to reconsider discussing some ot
about posting guidelines (which i have read --- before i posted my
first msg) in this thread ...

Paul Lalli got it all right, i want to basically binary-add up
fixed-length ascii-strings which contain '0' and '1', character-wise
(10 + 11 = 10).

Anyway, my need has changed a bit (in fact the adding technique didn't
work ... *dough*). I need to have a 1 in the output string, if _the
majority of remaining strings_ (more than half) has a 1 there. My
attempt is to simply iterate through the indices $i of the strings and
then add up (arithmetically) all $i'th characters in all strings. If
they amount to more than half of the number of strings, i'll write a 1.
I didn't implement it yet (not working right now).

Just to clarify: I am indeed capable of writing such trivial snippets
myself. Yet in my (very recent) experience with perl i have learned,
that there are A LOT of very neat and short ways of doing such things.
Additionally If your project is supposed to scale properly you also
need to consider the speed of your implementation of such frequently
used code-snippets ... I think it's fun to think of such snippets which
are small, fast and do a lot, that's why I posted ...

Perhaps you would find it easier to use Ruby.
Let's start by creating some data:

a = []
6.times { a << "%08b" % rand(256) }
puts a; puts

10011001
00001101
10100010
01101101
10000011
11100101

To make counting easier, let's swap columns and rows:

b = a.map{|x| x.split('') }.transpose.map{|x| x.join}
puts b; puts

101011
000101
001101
100000
110100
010101
001010
110111

Now we'll do the counting and produce the result-string:

result = ""; half = a.size / 2
b.each{|s| result << ( s.count("1") > half ? "1" : "0") }
puts a, "-" * result.size, result

10011001
00001101
10100010
01101101
10000011
11100101
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top