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

Dear Perl Monks, I have a problem in parsing the array.. for e.g, my array = {PSTR[0], PSTR[1], PSTR[2],PSTR[3],PSTR[4],PSTW[0],PSTW[1],PSTW[2],PSTW[3],PSTW[4],PWWD[3],PWWD[2],PWWD[1],PWWD[0]}; i would like to parse this array and get the all PSTR's PWTW's and PWWD's together and find the which is the first and last element of PSTR's, PSTW's and PWWD's Thanks in advance,

Replies are listed 'Best First'.
Re: parsing an array
by moritz (Cardinal) on Oct 10, 2010 at 08:23 UTC
    I'd start with maybe stripping the curly braces, and then splitting the string on /\s*,\s*/.

    Iterating over the result list should give you all the information you need.

    See also: perlretut, perlintro.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: parsing an array
by GrandFather (Saint) on Oct 10, 2010 at 08:31 UTC

    Dear OP, I have a problem reading your code.

    I would like to help, but without some code to read it's rather hard to see where you are having trouble. Thanks in expectation.

    True laziness is hard work
Re: parsing an array
by eyepopslikeamosquito (Archbishop) on Oct 10, 2010 at 08:36 UTC
Re: parsing an array
by LanX (Saint) on Oct 10, 2010 at 11:15 UTC
    Unclear! Do you wanna parse a string or do you wanna search within an array?

    The given code is no perl.

    Cheers Rolf

Re: parsing an array
by snape (Pilgrim) on Oct 11, 2010 at 03:18 UTC

    The way you have defined the array doesn't looks correct to me. If you are talking about an array

    my @array = ("PSTR_0", "PSTR_1", "PSTR_2","PSTR_3","PSTR_4", "PSTW_0 +","PSTW_1","PSTW_2","PSTW_3","PSTW_4", "PWWD_3","PWWD_2","PWWD_1","PWWD_0");

    then have a look to my program. I think it will give you what you have asked.

    #!/usr/bin/perl use strict; use warnings; my @array = ("PSTR_0", "PSTR_1", "PSTR_2","PSTR_3","PSTR_4", "PSTW_0 +","PSTW_1","PSTW_2","PSTW_3","PSTW_4", "PWWD_3","PWWD_2","PWWD_1","PWWD_0"); my @PSTR = (grep{m/PSTR_\d/}@array); print @PSTR,"\n"; my @PSTW = (grep{m/PSTW_\d/}@array); print @PSTW,"\n"; my @PWWD = (grep{m/PWWD_\d/}@array); print @PWWD,"\n"