You can use a hash to keep track of how many times you've seen each occurance and base the name on that. I'm assuming that you want to create unique file names based on the first token on each line? If so, here's a snippet that will do that. It's not your implementation but it should be self explanatory enough for you to adapt it.
#!/usr/bin/perl
use strict;
use warnings;
my %index;
my @data = qw/1 2 3 4 3 4 5 1 11 11 11 11 11 3/;
my $filename;
for (@data) {
if ($index{$_}) {
$filename = $_ . $index{$_};
$index{$_}++;
}
else {
$filename = $_;
$index{$_} = 'a';
}
print "Your filename is $filename.\n";
}
The output was..
Your filename is 1.
Your filename is 2.
Your filename is 3.
Your filename is 4.
Your filename is 3a.
Your filename is 4a.
Your filename is 5.
Your filename is 1a.
Your filename is 11.
Your filename is 11a.
Your filename is 11b.
Your filename is 11c.
Your filename is 11d.
Your filename is 3b.
You'll run into issues if you have more occurances of one token than there are letters in the alphabet though.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.