keiusui has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am programming a website script in Perl that enables users to add other users as friends. Every user has a unique ten-digit ID number. However, I am not sure what the best way to keep track of everyone's friends is.

I believe the best way to keep track of everyone's friends may be to use a node-segment data structure.

Does anyone know of a node-segment or similar data type that can be used by Perl?

Thank you for your help.

  • Comment on best way to keep track of who's friends with whom

Replies are listed 'Best First'.
Re: best way to keep track of who's friends with whom
by ELISHEVA (Prior) on Oct 05, 2009 at 07:03 UTC

    Are you asking about how to work with the data when it is in memory or how to store it long term for each user?

    Long term storage is normally handled by a database and you would likely use a cross reference database table whose primary key includes the ids of both friends. To make sure you don't double add friend pairs you would need a rule for deciding which of any two friends goes in the first key field and which goes in the second key field.

    If you already know how you are storing your friends pairs, but just want to know a good data structure for manipulating the information in memory, then you will need to provide more information. The best data structure will depend on how you are planning to use the data.

    Best, beth

      To make sure you don't double add friend pairs you would need a rule for deciding which of any two friends goes in the first key field and which goes in the second key field.
      ...assuming that friendship is commutative. (i.e., "If Alice likes Bob, then Bob likes Alice.") If it's directional, as on LiveJournal or Twitter, then this would not apply, since "Alice likes Bob" and "Bob likes Alice" are independent of each other.
        Totally agreed.

        In case you do want to treat friendship as commutative, here's a simple rule: require that the value of the id A is numerically smaller than the value of id B. If it's the other way around, swap them.

      beth, that's a interesting way you mention. (Sounds like a question of storage, not execution time handling.)

      I think what Beth is suggesting deals with data normalization techniques- where you break down everything into the smallest most simple tables possible(?).

      It makes it so the data is not duplicated, it makes your sql queries a little more complicated (a lot), but the whole thing works better.

      Basically your friend people table:

      person_id name
      1   joe
      2   leo
      3   jimmy
      

      And your friend table:

      person_a_id person_b_id
      1           2
      1           3
      

      This shows that joe is friends with leo and jimmy- but leo and jimmy are not friends.

Re: best way to keep track of who's friends with whom
by jethro (Monsignor) on Oct 05, 2009 at 09:40 UTC
    What do you mean by node-segment?

    You could store it as a lists of friends-IDs accessible by ID, in perl that would be a Hash of Arrays (or even a Hash of Hashes).

    Or as nodes of IDs with pointers to any friend-node, which is maybe what you meant with node-segment. But although pointers aka links are available in perl and could be stored in a Hash of Arrays too, it has no advantages over the first mentioned approach that I can see. And it is less natural to use in perl. And you would have to store the ID of the user into the first entry of the array so you know who it is when you access a friends list coming from a link instead of the hash, which is mayor badness and a recipe for creating inconsistent data structure

    The first approach also translates naturally to database storage in a relational database.

    Examples of using the data structure Hash of Arrays can be found here and here in perldoc