@str= qw(ssg.nal ssg_00.nsr ssg_00.nsn ssg_00.nss);
I need a find unique pattern 'ssg*' from @str , so that i can calculate the size of ssg* directly.
it could be like $size{ssg*} = 1
Can anyone please help me ?
- kulls
What is ur required output for the above example data ?
Regards,
S.Venni Rajan.
"A Flair For Excellence."
-- BK Systems.
my %size;
for(@str) {
$size{$1}=1 if($_ =~ /(gss)/);
}
i need to handle (but i didn't in my example) '.' and '_' in my pattern 'gss' in order to indentify unique patterns.
For more details,
i need 'gss' as a unique pattern, instead of 'gss.' , 'gss_'
Sorry, but I think that you are going to have to clarify your question.
You have an array of strings @str
You want to search the strings in that array and locate those that contain (or start?) with the substring 'ssg'?
Note: What does the '*' signify? Is it a literal '*', or are you using it to indicate 'ssg' + plus more, as in a DOS style wildcard? In a perl regex that would have to be /ssg.*/; note the '.'
Also note that if it is a literal, and $size{ssg*} = 1; is a hash assignment, then you will need to quote any key containing non-word characters: $size{ 'ssg*' } = 1;; but if it is a wildcard, you cannot use wildcards with (standard) hashes.
And where does size come in? Do you want
If you're always looking for prefixes, then something like this may get you the pattern:
sub prefix {
my $prefix = shift;
for (@_)
{
next if $prefix eq substr($_, 0, length $prefix);
chop $prefix;
redo;
}
$prefix;
}
Then you just put a * at the end of whatever it returns. If you want to find the longest shared string, well, that would be a bit more difficult.
I have added to the data to illustrate the point.
my @str= qw(ssg.nal ssg_00.nsr ssg_00.nsn ssg_00.nss xxx_01.txt xxx_03.dat);
my %starts;
foreach (@str) {
#increment the value for the hash element that has the
#prefix as a key.
$starts{$1}++ if /^([a-zA-Z0-9]+)/;
}
# print all the prefixes and number of files associated
foreach (keys %starts) {
print "$_ = $starts{$_}\n";
}
gives
ssg = 4 xxx = 2
Let me know if this is what you had in mind.
perlmonks.org content © perlmonks.org and BrowserUk, inman, kulls, Tanktalus, vennirajan, ysth
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03