Solved

Perl grep from array

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now