Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:25:50+00:00 2026-05-25T10:25:50+00:00

This works but is uglier than hell, basically it’s iterating through two separate portions

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:25:50+00:00Added an answer on May 25, 2026 at 10:25 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Delphi TApplication.OnException Event to catch unhandled exceptions This works well but
(Citing source at: http://jqueryui.com/demos/dialog/#modal-form ) As an example, this works great but each time
I use the line below in my C# winform app, this works great but
$.post($(this).attr(action), $(this).serialize(), function(json) { alert('red'); AddItemAjax(json); }, json); This works in Chrome but not
Why doesn't this work <script src=jquery.js/> But this works <script src=jquery.js></script> ? Firefox 3.5.8
This works perfectly in Firefox but doesnt work in ie i get the following
This works, but is it the proper way to do it??? I have a
This works : alert(document.getElementById(Container).nodeName); But this doesnt : var CurParent = document.getElementById(Container); alert(CurParent.nodeName); I
This works in jQuery 1.3.2, but not in 1.4 $(#container).children().map(function() { var child =
This works: Tweener.addTween( [ _fee, _fye, _fum ] , { alpna:1, time:10 }); But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.