kulls has asked for the wisdom of the Perl Monks concerning the following question:

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 ?

Update more : i just want to get the unique 'ssg' alone and i can add '*' pattern in future for getting the size.
So i can get  $size{ssg} = 1 like this to process further.
- kulls

Replies are listed 'Best First'.
Re: Identifiying unique pattern
by BrowserUk (Patriarch) on Jan 06, 2006 at 04:40 UTC

    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
by Tanktalus (Canon) on Jan 06, 2006 at 04:41 UTC

    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
by vennirajan (Friar) on Jan 06, 2006 at 04:31 UTC
    Hey kulls,

    What is ur required output for the above example data ?

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

      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
by inman (Curate) on Jan 06, 2006 at 12:58 UTC
    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_0 +3.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.

Re: Identifiying unique pattern
by ysth (Canon) on Jan 06, 2006 at 10:06 UTC
    Still unclear what you want. What exactly would end up in the %size hash given your example array?