Essentially, I made a bunch of Perl code that currently produces output like this:

```none
if: 12
for: 26
rof: 26
fi: 27
for: 25
rof: 25
fi: 27
...some more backtracking...
if: 9
fi: 9
for: 26
rof: 26

```

Each line is an event generated by code in a regex in the form: `title: position`.

(I'm not asking about the regex; it perfectly describes a simple CFG I made up. Don't mention anything about it, I just need help storing and then retrieving the data that it's already printing.)

I want `sub mark` to put this data into a data structure. But I'm not sure the best way to do this. I need to be able to retrieve the data in three ways, (with an example of each):

* Title: What events are titled `for`?
* Position: What events fired off at position 12?
* Order: What event was last?

I also want to avoid duplicate entires. If `fi: 27` happens as the fourth and eighth event, I want one entry, with order being an array like `4,8,`. 

Please keep in mind that I am very inexperienced with Perl and I would appreciate code examples (explaining how to both store and retrieve using iteration). I know that there seems to be three types of data structures: `$`, `@`, and `%`. I haven't gotten much further than that, because Perl is structured completely different than Java (which I have the most experience in).

Feel free to ask me to clarify anything.

My code is very simple, except for the regex:

```
sub mark{
    my ($title, $position)= @_;
    print "\n";
    print $title;
    print $position;
}

$re = qr{
((?&line))

|(*F)(?:
   (?'line'  (?&if)|(?&for))
   (?'if'    if \((?&exp)*\)\{\s*(?{mark("if: ", pos)})(?&line)*(?{mark("fi: ", pos)})\s*\})
   (?'for'   for\((?&exp)*\)\{\s*(?{mark("for: ",pos)})(?&line)*(?{mark("rof: ",pos)})\s*\})
   (?'eq'    [=])
   (?'exp'   (?&abc)(\s?(?&eq)\s?(?&num)+)?)
   (?'num'   [0-9])
   (?'abc'[a-z])
   (?'space' \s)
)}xm;

# I've been changing the following 'string' to test different stuff
# This is the run that produced the sample data
# 'oops;' doesn't follow the grammar rules, so it shows how the regex may backtrack

'if(true){
  for(i = 5){
  }
  oops;
}' =~ /$re/
     and print "\n\n$1\n\n";

```