-2

I have like a month with this problem. I want to check in my file all the words.

EXAMPLE FILE:_word [0] word1 [1] word1 [2] word2 [3] _word [4]

That will be the positions, but i want that if i see the word:_word in the position 0, put that position 0 to the word in the position 4 that is the same ("_word") duplicate.

I have try a lot with this problem.

I want to do something like this:

EXAMPLE :_word [0] word1 [1] word1 [1] word2 [2] _word [0]

The [number] are the positions.

  • 2
    What have you tried so far? Your description is really confusing, and I might be able to understand the problem better if I see the code you've attempted to write. – Matt Jacob Nov 02 '15 at 19:58
  • It's still not clear. that's a similar question, but it is different. And for another programming language. What have you tried so far. Can you put your `EXAMPLE` outside of the codeblock and show exactly what you expect the input and the output to be? – Patrick J. S. Nov 02 '15 at 20:42
  • Ok, for example in this link http://stackoverflow.com/questions/5419204/index-of-duplicates-items-in-a-python-list I have a list List = ['A', 'B', 'A', 'C', 'E']; I want to get something like this: index 0: A index 2: A but in here instead of the 2, put 0 again..because its the same word... – Shinmi Benjamin Nov 02 '15 at 20:45
  • So, since there's no code, can I assume you haven't tried anything yet? Are you looking for help with the algorithm? – Matt Jacob Nov 02 '15 at 21:00
  • It sounds like you want to remove all duplicate elements except the first from an array, e.g. `('A', 'B', 'A', 'C', 'B')` -> `('A', 'B', 'C')`. Is that right? – ThisSuitIsBlackNot Nov 02 '15 at 21:17

1 Answers1

0

Assuming, given the string "_word word1 word1 word2 _word", you want to come up with the array (0,1,1,3,0) -- for the first index of each word, then you could do:

$line = "_word word1 word1 word2 _word";
@words = split " ", $line;
$first_index{$words[$_]} //= $_ for 0..$#words;
push @indices, $first_index{$_} for @words;
glenn jackman
  • 238,783
  • 38
  • 220
  • 352