views:

68

answers:

2

Hi All, I am having a weird mind gogling problem. Sorry in advance, If I confuse you.

I have following data

1 3 5
5 1 2
2 4 8
3 2 9
3 8 4

first column = source 1
second column = source 2
third column = result column

First and second column will can create 3rd column and that 3rd column can be used in 1st or 2nd column to make a new 3rd column value. You can see in first row 5 was 3rd column and in 2nd row it was input to make 2 (3rd column)

I am looking for a solutin that if it pass 3 it return me (1,4,5) rows and same for other digits. Solution tip can be either in java or php because I just want to get data from a old project to make it work !

Thanks !

EDIT


You all guys are all great ! ! ! Thanks for reading my question and I am extremely sorry for confusion.

I am making hierarchical clustering stuff to cluster some old data. I used a java class that generated this output. column1 & column2 are source and column3 is result. In hierarchical clustering you merg data and it create another new node and you keep doing it till it return just one root node.

If any body know a good hierarchical cluster in java or php just let me knkow. I know some packages but I don't like BIG APIs

+2  A: 

Assuming the data is in plain text, the first thing that you'll need to do is process the data in order to make it queryable.

Approach

  • Loading the data from the file
  • Splitting the file by the newline character into rows
  • Splitting the rows into columns by the space character

From here, it's a simple case of looping through your queryable data in search of the values you're looking for.

Possible Solution

An object-oriented solution in PHP may look a little something like this:

class myData {

    var $data;

    function myData($d) {
        $this->data = $d;
    }


    function search($s) {

        $results = array();

        // loop through our pre-loaded data
        foreach($this->data as $key => $dataRow) {

            // if the value we are looking for is in the data array
            if(in_array($s, $dataRow)) {

                // add it to the results, 
                // keeping the associative key (0, 1, 2, 3 ...)
                $results[$key] = $dataRow;

            }

        }

        return $results;
    }


    static function load($f) {

        $contents = file_get_contents($f);

        // split the data by new line
        $dataRows = explode("\r\n", $contents);

        $data = array();


        foreach($dataRows as $key => $dataRow) {

            // split each row by the space character
            $data[$key] = explode(" ", $dataRow);


        }

        return new myData($data);           

    }

}

Usage example

Assuming the data file is called data.txt, usage is as follows:

$data = myData::load("data.txt");

$results = $data->search("3");

// display our result keys
echo "Found that in " . implode(array_keys($results), ", ") . "\r\n";

// or print the full results
print_r($results);

Results

The output for the above example:

Found that in 0, 3, 4
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
        )

    [3] => Array
        (
            [0] => 3
            [1] => 2
            [2] => 9
        )

    [4] => Array
        (
            [0] => 3
            [1] => 8
            [2] => 4
        )

)

Hopefully I understood your question properly, and this at least helps you eventually achieve the result you're looking for.

Bauer
+1 is given !, you understood my question 100%. Your solution looks good. I am going to implement now ! ! ! i will update you :)
Wow! +1 to Bauer!!!
polygenelubricants
+1  A: 

It took a few times reading this, but I think I know what you are trying to ask. So if I understand correctly, these examples would work too: if you are returned 2, you would get rows (2, 3, 4) if you are returned 8, you would get rows (3, 5)

So in order to answer this question, we will need to know what kind of data structure the numbers are in. Here is an example that is quick and dirty.

// original data sets
$column1 = array(1,5,2,3,3);
$column2 = array(3,1,4,2,8);
$column3 = array(5,2,8,9,4);

$number_passed = 3;
$count = count($column1)-1;

for($i = 0; $i <= $count; $i++)
{
    if($column1[$i] == $number_passed or
       $column2[$i] == $number_passed or
       $column3[$i] == $number_passed){
        $columns[] = $i+1;
    }
}
print_r($columns);

If you provide more information as the original structure, I could possible provide better code. Hope it helps.

Happy Coding!

cdburgess
i love php ! ! !yes you understood my question correct. Sorry for confusion.