Anyway to simplify this rats nest of foreach loops?

為{幸葍}努か 提交于 2019-12-08 01:38:56

问题


This works but is uglier than hell, basically it's iterating through two separate portions of a sub array, seeing if there's a greatest common denominator besides 1 in the values of both sub arrays, and if there is, multiplying the base value by 1.5

Sorry for the sloppy code ahead of time.

error_reporting(E_ALL);
ini_set('display_errors', '1');

class CSVParser
{

    public $output = NULL;
    public $digits = NULL;

    public function __construct($file)
    {


        if (!file_exists($file)) {
            throw new Exception("$file does not exist");
        }

        $this->contents = file_get_contents($file);
        $this->output = array();
        $this->digits = array();
        $this->factor = array();
    }

    public function parse($separatorChar1 = ',', $separatorChar2 = ';', $enclosureChar = '"', $newlineChar = "\n")
    {

        $lines = explode($newlineChar, $this->contents);
        foreach ($lines as $line) {
            if (strlen($line) == 0) continue;
            $group = array();
            list($part1, $part2) = explode($separatorChar2, $line);
            $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part1), array("$enclosureChar \t"));
            $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part2), array("$enclosureChar \t"));
            $this->output[] = $group;
        }
    }

    private function trim_value($value, $chars)
    {
        return preg_replace("#^( |" . $chars . ")+#", '', $value);
    }


    private function gcd($x,$y) 
    {
        do {
            $rest=$x%$y;
        $x=$y;
        $y=$rest;
        } while($rest!==0);
        return $x;
    }

    public function algorithm()
    {
        $alpha = array(
            'c' => str_split('bcdfghjklmnpqrstvwxz'),
            'v' => str_split('aeiouy')
        );
        $i=$k=0;
        foreach ($this->output as $item) {
            $cnt = 0;
            $this->digits[$i] = array();
            foreach ($item as $part) {
                $this->digits[$i][$cnt] = array();
                $new = array();
                foreach ($part as $str) { 
                    $v = count(array_intersect(str_split($str), $alpha['v']));
                    $c = count(array_intersect(str_split($str), $alpha['c']));
                    $t = strlen(str_replace(' ', '', $str));

                    $new = ($cnt == 0) 
                        ? array('v' => $v, 'c' => $c, 't' => $t, 'm' => ($t%2) ? $v * 1.5 : $c) 
                        : array('v' => $v, 'c' => $c, 't' => $t);

                    $this->digits[$i][$cnt][] = $new;
                }
                $cnt++;
            }
            $i++;
        }
        $h=$cuml=0; 
            foreach($this->digits as &$slice) { 
            foreach($slice[0] as &$sliceName){ 

                foreach($slice[1] as $sliceProduct) { 
                foreach($sliceProduct as $pKey=>$pVal) { 

                    foreach($sliceName as $nKey=>$nVal) { 
                        $tmp[$h] = ($this->gcd($pVal,$nVal) != 1) ? ++$cuml:'';
                    } 
                } 
                    $tmp[$h] = $sliceName['m']*$cuml*1.5; 
                    $h++; 
                    $cuml=0; 
                }$h=0; 

            $sliceName['f'] = $tmp; 
            $tmp=''; 
            } 

            } 
        foreach($this->digits as &$u){unset($u[1]);} 
    } 

}

$parser = new CSVParser("file.csv");
$parser->parse();   //print_r($parser->output);
$parser->algorithm();   print_r($parser->digits);

Sample CSV per request

Jeff Goes, Mika Enrar;Triple Threat, Dogs on  Bikes
Sonny Ray, Lars McGarvitch, Jason McKinley;Kasabian, Lords of Acid, Hard-Fi

The Output

Array
(
    [0] => Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [v] => 3
                        [c] => 3
                        [t] => 8
                        [m] => 3
                        [f] => Array
                            (
                                [0] => 40.5
                                [1] => 4.5 // Remainder.. So 'Jeff Goes' => 'Dogs on Bikes'
                            )

                    )

                [1] => Array
                    (
                        [v] => 3
                        [c] => 4
                        [t] => 9
                        [m] => 4.5
                        [f] => Array
                            (
                                [0] => 67.5 // High Score! So 'Mika Enrar' => 'Triple Threat'
                                [1] => 13.5
                            )

                    )

            )

    )

    [1] => Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [v] => 4
                        [c] => 2
                        [t] => 8
                        [m] => 2
                        [f] => Array
                            (
                                [0] => 24
                                [1] => 12
                                [2] => 24 // Next Highest 'Sonny Ray' => 'Hard-Fi'
                            )

                    )

                [1] => Array
                    (
                        [v] => 3
                        [c] => 8
                        [t] => 14
                        [m] => 8
                        [f] => Array
                            (
                                [0] => 84 // High Score! (This is really a tie, but 'm' has the highest secondary value so...) 
                                [1] => 60 // 'Lars McGarvitch => 'Kasabian'
                                [2] => 84
                            )

                    )

                [2] => Array
                    (
                        [v] => 5
                        [c] => 5
                        [t] => 13
                        [m] => 7.5
                        [f] => Array
                            (
                                [0] => 0
                                [1] => 0 // The only one left 'Jason McKinley' => 'Lords of Acid'
                                [2] => 11.25
                            )

                    )

            )

    )

)

What it does

What this class does so far is split the csv one array, split content prior to ; and after into two sub arrays. Count the consonants and vowels of both, find if there is a greatest common denominator between the two subsections for each C V or mixed letter pair, and create a value to assign a band to a product.

What really needs to do though

The highest value generated should be associated with the band that created that high value. So what I am trying to really do is associate a name to a band depending on how high of a score it ultimately generates. I'm about half way through =(

As you guys can see, this code is a mess, literally. All I really want is to assign a name to a band based on the numbers I'm generating.


回答1:


I have to agree with everyone else here... but I'd like to add:

Instead searching for how to traverse $this->digits more simply, you should strongly consider rethinking the structure of the data in $this->digits.

Furthermore, lumping everything into a single array doesn't always make sense. But when it does, the structure can be thought out so that it is intuitive and can be traversed easily.

Without more information about what this is doing, there is no way for us to suggest how to restructure your data / class. A start would be giving us what a sample $this->digits array looks like. Also, some more information about your problem would be good (like how this method is used).




回答2:


If it works why are you changing it? Performance? Refactor? Business changed? Requirements changed? Clean code Samaritan? Boy Scout rule?

When I come across "spaghetti code" I leave it alone unless I absolutely must change it. That said, I would write a couple of unit tests verifying the output of the "spaghetti code" so that I know that I did not break anything or make things worse.



来源:https://stackoverflow.com/questions/7249016/anyway-to-simplify-this-rats-nest-of-foreach-loops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!