Identifiying unique pattern
kulls
created: 2006-01-05 23:24:58
Greetings,
I have a string array ,
   @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
Re: Identifiying unique pattern
created: 2006-01-05 23:31:33
Hey kulls,

What is ur required output for the above example data ?

Regards,
S.Venni Rajan.
"A Flair For Excellence."
                -- BK Systems.

Re^2: Identifiying unique pattern
created: 2006-01-06 03:37:56
something like,
   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_'
Re: Identifiying unique pattern
created: 2006-01-05 23:40:51

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

  1. The number of strings in @str that contain (or start?) with 'ssg'?
  2. The length of (each) string that contains 'ssg'?
  3. Something else entirely?

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Identifiying unique pattern
created: 2006-01-05 23:41:50

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.

Re: Identifiying unique pattern
created: 2006-01-06 05:06:27
Still unclear what you want. What exactly would end up in the %size hash given your example array?
Re: Identifiying unique pattern
created: 2006-01-06 07:58:21
I have interpreted the question as follows: Given a set of file names in an array, find the number of files that share the same alphanumeric prefix so that I know how many files I will be deling with when I use $prefix* as a glob.

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