find similar characters in strings?

Discussion in 'Perl Misc' started by jonas.huckestein@web.de, Sep 22, 2005.

  1. Guest

    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

    --
    greetings, jonas
     
    , Sep 22, 2005
    #1
    1. Advertisements

  2. Paul Lalli Guest

    wrote:
    > 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
     
    Paul Lalli, Sep 22, 2005
    #2
    1. Advertisements

  3. wrote:
    > 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 22, 2005
    #3
  4. Paul Lalli Guest

    Gunnar Hjalmarsson wrote:
    > wrote:
    > > 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
    >


    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
     
    Paul Lalli, Sep 22, 2005
    #4
  5. Paul Lalli wrote:
    > Gunnar Hjalmarsson wrote:
    >> 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.


    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. :(

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 22, 2005
    #5
  6. Dr.Ruud Guest

    [OT] how to create a useful post (was: Re: find similar characters in strings?)

    Paul Lalli schreef:

    > 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...


    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.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 23, 2005
    #6
  7. Babacio Guest

    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.

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Sep 23, 2005
    #7
  8. Re: [OT] how to create a useful post

    Dr.Ruud wrote:
    > Paul Lalli schreef:
    >> 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...

    >
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 23, 2005
    #8
  9. Guest

    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 ...

    --
    greetings, jonas
     
    , Sep 23, 2005
    #9
  10. Paul Lalli Guest

    wrote:
    > ok guys,


    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
     
    Paul Lalli, Sep 23, 2005
    #10
  11. Babacio Guest

    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.

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Sep 23, 2005
    #11
  12. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:

    > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Sep 23, 2005
    #12
  13. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Sep 23, 2005
    #13
  14. wrote:
    > 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".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 23, 2005
    #14
  15. wrote:
    > 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
    --------
    10000001
     
    William James, Sep 25, 2005
    #15
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davor Cengija

    Finding similar strings

    Davor Cengija, Jan 5, 2004, in forum: Java
    Replies:
    5
    Views:
    664
    Roedy Green
    Jan 5, 2004
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    939
    Malcolm
    Jun 24, 2006
  3. Laszlo Nagy
    Replies:
    6
    Views:
    779
  4. Terry Reedy
    Replies:
    0
    Views:
    657
    Terry Reedy
    Jul 1, 2008
  5. M.-A. Lemburg
    Replies:
    0
    Views:
    1,041
    M.-A. Lemburg
    Jul 2, 2008
Loading...

Share This Page