Solved

Perl grep from array

Posted on 2011-03-11
2
382 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

749 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