0

I'm trying to parse a number of "filename.ext" comming from a file. This exercice comming from CodinGames named "Mime type".

I should take the name of each file, look at his extension, and find the matching key => value in the extensions array.

E.g: for a file named "foobar.pdf", I look if there's a dot, and if it is, I split the name in the files array [foobar => pdf]. If there's no dot like "foobar", it becomes [foobar => Unknown]. Last case asked, if there are two dots, "foo.bar.pdf" is pushing like [foo.bar => pdf] but I'm not here for that case.

My problem is that my first file name is "a" with no dot no extension. So the code runs and does it well. It ends with the perfect input in the files array [a => Unknown]. But now, the second file name is "a.wav". And the program will replace the value of [a => Unknown] to [a => wav] cause it's the same key.

So how can we avoid these replacements ? I searched on web, but everybody are searching to replace key value, not create a new one.

edit: What I expect is have 2 differents input in my file array. In this case I want: $files = [ [a => Unknow], [a => wav], ... ]. After this I must output each mime type comming from the extensions array for each file, so in order: "Uknown", "audio/x-wav", ... this is why I must keep the first key, because there's no extension and it's good to know.

$FNAME = "a
          a.wav
          b.wav.tmp
          test.vmp3
          pdf
          .pdf
          mp3
          report..pdf
          defaultwav
          .mp3.
          final.";

There is my code:

$N = 4 // Number of extensions
$Q = 11 // Number of files to parse   

$extensions = [
                  [wav] => audio/x-wav
                  [mp3] => audio/mpeg
                  [pdf] => application/pdf
                  [UNKNOW] => UNKNOWN
              ];


$files = [];

for ($i = 0; $i < $Q; $i++){

    // Input / One file name per line.
    $FNAME = stream_get_line(STDIN, 256 + 1, "\n");
    // --- TRANSFORM STRING INTO KEY => VALUE ---
    // If there is 2 dots e.g "file.name.ext"
    if (preg_match('/\..*\./',$FNAME)){
        // Do something e.g split string at the second dot, I don't know for the moment how to do this
        // I tried regex option U but something wrong happen;
    }

    // If there is one dot in "filename.ext"
    elseif ( preg_match("/\./",$FNAME) ){ 
        $temp = preg_split ("/\./", $FNAME); 
        // Verification if extension is know
        if ( array_key_exists( $temp[1], $extensions) ){
            $files[$temp[0]] = $temp[1];
        } else {
            $files[$temp[0]] = "UNKNOW";
        }
    } else {
        $files[$FNAME] = "UNKNOW";
    }
}
6
  • 2
    What do you mean by "avoid"? What would you expect to happen? Commented Mar 9, 2020 at 15:02
  • ...if you expect that two items are stored under the key a (one with unknown, one with .wav): this won't work Commented Mar 9, 2020 at 15:02
  • 1
    So how do you want to save these 2 occurances that have the same key? Commented Mar 9, 2020 at 15:10
  • Why are you using the filename part before the suffix as array key in the first place, if that can occur multiple times? That makes rather little sense to begin with. Commented Mar 9, 2020 at 15:10
  • @RiggsFolly yes. Commented Mar 9, 2020 at 15:33

2 Answers 2

1

With these statements, you'll overwrite the value of that key each time.

$files[$temp[0]] = $temp[1];
$files[$FNAME] = "UNKNOW";

Just use [] to append instead.

$files[$temp[0]][] = $temp[1];
$files[$FNAME][] = "UNKNOW";

By the way, I think you can use this to handle the final extension if there are multiple extensions

preg_match('/(?<=\.)[^.]+$/', $test, $ext);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I didn't want to have two dimensional array cause it's more complicate for the next part of my exercice. But I think it's the only solution to not overwrite value from dupplicate keys
@John_Underscore if you want to avoid having a 2d array, you could append to the string value instead with $files[$temp[0]] .= $temp[1]; etc. Doing it that way has its own complications, though. You'll need to check if a value exists before appending, find a suitable delimiter, etc.
0

what you are trying to do don't make sense to me, but still you can achieve that by replacing your inner if else block with following.

// Verification if extension is know
if ( array_key_exists( $temp[1], $extensions) ){
    if(!in_array($temp[0], array_keys($files))){
        $files[$temp[0]] = $temp[1];   
    }
} else {
    if(!in_array($temp[0], array_keys($files))){
        $files[$temp[0]] = "UNKNOW";   
    };
}

1 Comment

Thank you for taking time for my problem, I don't have time right now to parse your statements, but I will do it later.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.