Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

How do I get only one item out of attribute?

by mr_evans2u (Novice)
on Aug 21, 2006 at 16:13 UTC ( #568591=perlquestion: print w/replies, xml ) Need Help??

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

In my Perl code I am pulling down an attribute called dn which has three items in it seperated by a comma. I need only the second item (ou=). How do I only show this one item? my $branch = $return{"dn"}; will return for example corpid=xxxxxx,ou=people,o=corp I don't want corpid=xxxx or o=corp
  • Comment on How do I get only one item out of attribute?

Replies are listed 'Best First'.
Re: How do I get only one item out of attribute?
by imp (Priest) on Aug 21, 2006 at 16:28 UTC
    The easiest way is to use a regular expression.
    my $dn = 'corpid=xxxxxx,ou=people,o=corp'; my ($ou) = $dn =~ /ou=([^,]*)/; print "ou = $ou\n";
    You could also create a hash of the tokens like this if you need to test other values:
    my $dn = 'corpid=xxxxxx,ou=people,o=corp'; my %tokens = map {split /=/} split /,/, $dn; print "ou = $tokens{ou}\n";
    This approach first splits the $dn variable into pairs of this form:
    It then splits each of those pairs by the '=', which results in this list:
    Which is equivalent to defining a hash like this:
    my %tokens = ( corpid => 'xxxxxx', ou => 'people', o => 'corp', );

      Another hint is use the following, which I think is more readable :-)

      my $dn = 'corpid=xxxxxx,ou=people,o=corp'; my ($ou) = $dn =~ /ou=(.*?),/; print "ou = $ou\n";

      From perldoc perlre:

      Sometimes minimal matching can help a lot. Imagine youíd like +to match everything between "foo" and "bar". Initially, you write somet +hing like this: $_ = "The food is under the bar in the barn."; if ( /foo(.*)bar/ ) { print "got <$1>\n"; } Which perhaps unexpectedly yields: got <d is under the bar in the > Thatís because ".*" was greedy, so you get everything between t +he first "foo" and the last "bar". Here itís more effective to use mini +mal matching to make sure you get the text between a "foo" and the +first "bar" thereafter. if ( /foo(.*?)bar/ ) { print "got <$1>\n" } got <d is under the >

      Igor 'izut' Sutton
      your code, your rules.

Re: How do I get only one item out of attribute?
by mscerra (Monk) on Aug 21, 2006 at 16:39 UTC
    The simplest way is to use split.
    my @results = split /,/, $branch; my $desiredAtt = $results[1];
    If you want the returned value to only be the ou value you might look into making it return a hash of hashes. Your return hash would point to another hash. The sub hash would have three keys, corpid, ou and o. Then you could call
    to only get the ou field.
      thanks, mscerra that worked!
Re: How do I get only one item out of attribute?
by swampyankee (Parson) on Aug 21, 2006 at 17:56 UTC

    If you are absolutely certain that the format position of this attribute won't change, my preference (like mscerra's) is to use split. Being slightly neurotic, I'd check whether the return value actually exists before printing it.

    If you're less certain about the postion, but still certain about the general formatting, a hash may be your best bet. This seems close to the spirit of your post. Something like this code may be a reasonable start:

    #!perl use warnings; use strict; my %attr; %attr = split(/\s*[,=]\s*/,'corpid=xxxxxx,ou=people,o=corp'); if exists $attr{ou} { print "value for ou is $attr{ou}\n"; } else { print "No value defined for ou...this may be an error\n"; }
    What's happening here is fairly simple: the string is being split on equal signs and commas, with the items to the left of the equal signs being used as the keys to a hash.


    Experience is a hard teacher because she gives the test first, the lesson afterwards.

    Vernon Sanders Law

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://568591]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2021-04-14 15:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found