Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Perl grep from array

Posted on 2011-03-11
2
376 Views
Last Modified: 2012-05-11

I have the following array @fimAttributes but have changed it to have two sections, one for blue and one for yellow in this example.

I was using a grep to preform a function based on if the attribute was in the array, but now that I have changed it to two sections in the array I'm not sure how to do the grep on the section that I want.

From the example below, how do I change the grep to grep the blue or yellow section of the array, something simple I'm sure, just not sure what I need to change the grep line to?

Thanks,

my @fimAttributes=(q{blue}=>[qw{
                            id
                            alias
                            c
                            cn
                            department
                            displayname
                            facsimiletelephonenumber
                            givenname
                            manager
                            jobtitle
                            userprincipalname}],
                    q{yello}=>[qw{  
                            givenname
                            jobtitle
                            l
                            mail
                            mailalias
                            mailalternateaddress
                            mobile
                            o
                            ou
                            userprincipalname}]
                );




my (@Attributes)=('cn','mail','manager','securitystatus','alias','itbuilding');

    my @Attributes1;
    foreach my $i (0..$#Attributes) {
      unless (grep( /^$Attributes[$i]$/i, @fimAttributes) ) {       
        push(@Attributes1,$Attributes[$i]);
      }
    }
    @Attributes=@Attributes1;

foreach my $att (@Attributes) {
  print "$att\n";
}

Open in new window

0
Comment
Question by:bt707
2 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 35111685
Change fimAttributes to be a hash, and then cast it where used on line 33:

my %fimAttributes=(q{blue}=>[qw{
                            id
                            alias
                            c
                            cn
                            department
                            displayname
                            facsimiletelephonenumber
                            givenname
                            manager
                            jobtitle
                            userprincipalname}],
                    q{yello}=>[qw{  
                            givenname
                            jobtitle
                            l
                            mail
                            mailalias
                            mailalternateaddress
                            mobile
                            o
                            ou
                            userprincipalname}]
                );




my (@Attributes)=('cn','mail','manager','securitystatus','alias','itbuilding');

    my @Attributes1;
    foreach my $i (0..$#Attributes) {
      unless (grep( /^$Attributes[$i]$/i, @{$fimAttributes{'blue'}}) ) {       
        push(@Attributes1,$Attributes[$i]);
      }
    }
    @Attributes=@Attributes1;

foreach my $att (@Attributes) {
  print "$att\n";
}

Open in new window

0
 

Author Closing Comment

by:bt707
ID: 35111707
That's pretty much what I tried but I see I had a mistake.

Thanks,
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question