Jump to content

Recommended Posts

I am trying to write a solution which sorts some output from one of our old legacy applications but I'm having some problems. I wrote a non OO implementation that works, albeit with some warnings but that's fine because at least I'm getting the result I want. Now that I've tried to move it over to an OO implementation nothing I want is happening, if anything but errors at all.

 

This is a sample file I wrote for testing purposes:

 

COL1	COL2	COL3	COL4	COL5
val1a	val2a	val3a	val4a	val5a
val1b	val2b	val3b	val4b	val5b
val1c	val2c	val3c	val4c	val5c
val1d	val2d	val3d	val4d	val5d
val1e	val2e	val3e	val4e	val5e
val1f	val2f	val3f	val4f	val5f
val1g	val2g	val3g	val4g	val5g

 

And here is the non OO code that works:

<?php

$file = file_get_contents("tableinput.txt"); 

$table = array();
$temp_table = array();
$file = explode ("\r\n", $file); //break the input into individual lines
                                 //we assume file was written on Windows machine (can easily change \r\n to simply \r for Linux)

foreach ($file as $line) { 
  
  $line = explode ("\t", $line);

  array_push($temp_table, $line); 
  
}

for($i = 0; $i < sizeof($temp_table); $i++){  
$col_array = array();                                                            
for($j = 1; $j < sizeof($temp_table); $j++){ 
	array_push($col_array, $temp_table[$j][$i]);
}
if($temp_table[0][$i] != "")
	$table[$temp_table[0][$i]] = $col_array;
}
//sort array based on data in COL1 in descending format.
//Since input data is already sorted in ascending order,
//this is the easiest way to show that this is effective
array_multisort($table['COL1'], SORT_DESC, SORT_STRING, $table['COL2'], $table['COL3'], $table['COL4'], $table['COL5']); 
echo "<pre>"; var_export($table); echo "</pre>"; //dump the sorted array to the screen (proof of concept)


?>

 

And here, alas, is the code that for reasons that are beyond me, does not.

 

<?php
class SortTable {
public $table = array();
  
  public function __construct ($filepath) {
              
    $file = file_get_contents($filepath); 

    $temp_table = array();
    $file = explode ("\r\n", $file); //break the input into individual lines
                                     //we assume file was written on Windows machine (can easily change \r\n to simply \r for Linux)
    
    foreach ($file as $line) { 
      
      $line = explode ("\t", $line);
    
      array_push($temp_table, $line); 
      
    }
    

    for($i = 0; $i < count($temp_table); $i++){ 

    	$col_array = array(); 
    	for($j = 1; $j < count($temp_table); $j++){ 
    		array_push($col_array, $temp_table[$j][$i]);
    	}
    	if($temp_table[0][$i] != "")
    		$this->table[$temp_table[0][$i]] = $col_array;
    }

  
  }
  
  public function GetSortedTable ($SortCol, $params) {
  
    /*$ParamSet = Array();
  
    array_push ($ParamSet, &$this->table[$SortCol]); 
    array_push ($ParamSet, explode(',', $params));
    
    foreach ($this->table as $column) {
        
        if ($column != $this->table[$SortCol]) {
        
          array_push ($ParamSet, &$column);
        
        } 
        
    } */
    
    //call_user_func_array('array_multisort', $ParamSet);


    array_multisort ($this->table['COL1'], SORT_DESC, SORT_STRING, $this->table['COL2'], $this->table['COL3'], $this->table['COL4'], $this->table['COL5']);
    
    return $this->table;
  
  }

}


$sort = New SortTable("http://root.pixomania.net/table.txt");

$table = $sort->GetSortedTable("COL3", "SORT_DESC, SORT_STRING");

echo "<pre>"; var_export($table); echo "</pre>"; //dump the sorted array to the screen (proof of concept)

?>

 

 

Here is the output from the non OO which works. This is exactly what I'm trying to get from the OO version (if I could manage to get rid of those warnings, that would be great, but beggars can sooooo not be choosers.)

 

Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 23

Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 23

Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21

Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 23

array (
  'COL1' => 
  array (
    0 => 'val1g',
    1 => 'val1f',
    2 => 'val1e',
    3 => 'val1d',
    4 => 'val1c',
    5 => 'val1b',
    6 => 'val1a',
  ),
  'COL2' => 
  array (
    0 => 'val2g',
    1 => 'val2f',
    2 => 'val2e',
    3 => 'val2d',
    4 => 'val2c',
    5 => 'val2b',
    6 => 'val2a',
  ),
  'COL3' => 
  array (
    0 => 'val3g',
    1 => 'val3f',
    2 => 'val3e',
    3 => 'val3d',
    4 => 'val3c',
    5 => 'val3b',
    6 => 'val3a',
  ),
  'COL4' => 
  array (
    0 => 'val4g',
    1 => 'val4f',
    2 => 'val4e',
    3 => 'val4d',
    4 => 'val4c',
    5 => 'val4b',
    6 => 'val4a',
  ),
  'COL5' => 
  array (
    0 => 'val5g',
    1 => 'val5f',
    2 => 'val5e',
    3 => 'val5d',
    4 => 'val5c',
    5 => 'val5b',
    6 => 'val5a',
  ),
)

 

You guys have always been great to me, I learn so much more every time I post here, so here's hoping you guys can point out all my retarded mistakes so I never have to make them again.

 

--David

 

 

using the PHP function file... you can eliminate your "explode by \r\n" method.. seeing as how file() will produce an array of every line... at which point you can use a foreach loop

 

Once you do that...

you can set the first loop to set your variables.. exploding by \t like you're already doing...

and then for every loop afterwards you can assign to each respective variable.

That doesn't explain why the OO implementation doesn't work. I appreciate your suggestion, I really do and plan to fix that once the OO version works.  I've had about 15 million people yell at me for that already. :P

Notice: Undefined offset: 1 in E:\wamp\www\Mike Broudy\tableproc.oop.php on line 26

Warning: array_multisort() [function.array-multisort]: Argument #4 is expected to be an array or a sort flag in E:\wamp\www\Mike Broudy\tableproc.oop.php on line 55

array (
  'COL1' => 
  array (
    0 => NULL,
  ),
  'COL2' => NULL,
  'COL3' => NULL,
  'COL4' => NULL,
  'COL5' => NULL,
)

 

It seems the issue is lying in that $table is not being accessed later within the class when GetSortedTable is called.

 

The underlying code in the two separate scripts is nearly identical, if not entirely for the most part, so in theory it *should* be working. But it's not, which I don't understand.

 

PHP may be a tad more relenting, provided you explode the file based on spec.... Change \r\n to \n and you should get this:

array (
  'COL1' => 
  array (
    0 => 'val1g',
    1 => 'val1f',
    2 => 'val1e',
    3 => 'val1d',
    4 => 'val1c',
    5 => 'val1b',
    6 => 'val1a',
    7 => NULL,
  ),
  'COL2' => 
  array (
    0 => 'val2g',
    1 => 'val2f',
    2 => 'val2e',
    3 => 'val2d',
    4 => 'val2c',
    5 => 'val2b',
    6 => 'val2a',
    7 => NULL,
  ),
  'COL3' => 
  array (
    0 => 'val3g',
    1 => 'val3f',
    2 => 'val3e',
    3 => 'val3d',
    4 => 'val3c',
    5 => 'val3b',
    6 => 'val3a',
    7 => NULL,
  ),
  'COL4' => 
  array (
    0 => 'val4g',
    1 => 'val4f',
    2 => 'val4e',
    3 => 'val4d',
    4 => 'val4c',
    5 => 'val4b',
    6 => 'val4a',
    7 => NULL,
  ),
  'COL5' => 
  array (
    0 => 'val5g',
    1 => 'val5f',
    2 => 'val5e',
    3 => 'val5d',
    4 => 'val5c',
    5 => 'val5b',
    6 => 'val5a',
    7 => NULL,
  ),
)

Okay, in my first post, I posted some non Object Oriented code that actually does work fine.

 

When I attempted to move the code over to an Object Oriented format, it broke. I didn't re-write the code, I just copy pasted with a few minor changes to variable declarations to seemingly make it work, however it does not work, at all, in the Object Oriented version.

 

Okay, in my first post, I posted some non Object Oriented code that actually does work fine.

 

When I attempted to move the code over to an Object Oriented format, it broke. I didn't re-write the code, I just copy pasted with a few minor changes to variable declarations to seemingly make it work, however it does not work, at all, in the Object Oriented version.

 

Why does using your OO code work for me after those changes?

 

You can't expect to copy and paste things into a class and call it object oriented and it to work.

I couldn't figure out why you were getting an invalid offset.. for multiple numbers..

so I used my boredom and wrote this out...

 

$file = file_get_contents("tableinput.txt"); 
$lines = explode ("\r\n", $file); //break the input into individual lines
//we assume file was written on Windows machine (can easily change \r\n to simply \r for Linux)

$rowCount = 0;
foreach ($lines as $line) { 
     if(!$rowCount)  $columns[] = explode ("\t", $line);
     else {
           $data[] = explode("\t", $line);
           $columns[$rowCount][] = $data[$rowCount];
     }
      $rowCount = ($rowCount }
echo "", print_r($columns), "";
?>

 

PHP may be a tad more relenting, provided you explode the file based on spec.... Change \r\n to \n and you should get this:

array (
  'COL1' => 
  array (
    0 => 'val1g',
    1 => 'val1f',
    2 => 'val1e',
    3 => 'val1d',
    4 => 'val1c',
    5 => 'val1b',
    6 => 'val1a',
    7 => NULL,
  ),
  'COL2' => 
  array (
    0 => 'val2g',
    1 => 'val2f',
    2 => 'val2e',
    3 => 'val2d',
    4 => 'val2c',
    5 => 'val2b',
    6 => 'val2a',
    7 => NULL,
  ),
  'COL3' => 
  array (
    0 => 'val3g',
    1 => 'val3f',
    2 => 'val3e',
    3 => 'val3d',
    4 => 'val3c',
    5 => 'val3b',
    6 => 'val3a',
    7 => NULL,
  ),
  'COL4' => 
  array (
    0 => 'val4g',
    1 => 'val4f',
    2 => 'val4e',
    3 => 'val4d',
    4 => 'val4c',
    5 => 'val4b',
    6 => 'val4a',
    7 => NULL,
  ),
  'COL5' => 
  array (
    0 => 'val5g',
    1 => 'val5f',
    2 => 'val5e',
    3 => 'val5d',
    4 => 'val5c',
    5 => 'val5b',
    6 => 'val5a',
    7 => NULL,
  ),
)

 

Probably because you're on Linux? I'm on Windows, but I've switched to the file() function anyway. :P

 

So, now it does seem to work. THe issue was that I was not assigning the final $table to the Object's $table, so $table was going out of scope for the rest of the class.

 

Everything works in the OO implementation now except for the sorting which now seems to be broken.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.