counting '.' in a domain name

Discussion in 'Perl Misc' started by Dr Eberhard Lisse, Feb 25, 2013.

  1. Hi,

    can someone point me to where I can read something about how I can
    count the '.'s in a domain name, ie lisse.NA = 1 and www.lisse.NA =
    2 and so forth, so I can weed out some subdomains from a list?

    greetings, el
    Dr Eberhard Lisse, Feb 25, 2013
    1. Advertisements

  2. Dr Eberhard Lisse

    Marc Girod Guest

    You don't mean...

    perl -le '
    @dn = qw(lisse.NA www.lisse.NA);
    for (@dn) {
    @f = /(\.)/g;
    print"$_: ", scalar @f;
    }'lisse.NA: 1
    www.lisse.NA: 2 you?
    Marc Girod, Feb 25, 2013
    1. Advertisements

  3. Dr Eberhard Lisse

    Dave Saville Guest

    my $domain = '';
    my $dots = () = $domain =~ m{\.}g;
    print $dots;
    Dave Saville, Feb 25, 2013
  4. * Dr Eberhard Lisse wrote in comp.lang.perl.misc:
    For this kind of question you can use the `perldoc` utility that ships
    with your Perl distribution like so:

    % perldoc -q count
    Found in perlfaq4.pod
    How can I count the number of occurrences of a substring within a
    There are a number of ways, with varying efficiency. If you want a
    count of a certain single character (X) within a string, you can
    use the "tr///" function like so:

    my $string = "ThisXlineXhasXsomeXx'sXinXit";
    my $count = ($string =~ tr/X//);
    print "There are $count X characters in the string";

    This is fine if you are just looking for a single character.
    However, if you are trying to count multiple character substrings
    within a larger ...
    Bjoern Hoehrmann, Feb 25, 2013
  5. Dr Eberhard Lisse

    Ivan Shmakov Guest

    [Cross-posting to news:free.comp.dns.]
    Nice, thanks!

    Please note, however, that in the most cases, the domain name
    may also include an insignificant [*] trailing dot. Thus, it's
    advisable to $domain =~ s/\.$// first.

    [*] In fact, such a trailing dot denotes the "root" of the DNS tree,
    thus distinguishing between relative ( and absolute,
    or fully-qualified, ( domain names. My
    opinion is, however, that relative DNS names are better never to
    be used, and what appears to be one is better to be treated as
    if it's an absolute one.
    Ivan Shmakov, Feb 25, 2013
  6. Thanks to all.

    The trailing dot I had already removed :)-O


    Dr Eberhard Lisse, Feb 26, 2013
  7. Perl has an idiom for counting characters in a string, namely, using
    the tr/// operator with an empty replacement list, as in

    perl -e '$a = ""; print $a =~ tr/.//, "\n";'
    Rainer Weikusat, Feb 26, 2013
  8. Thanks.


    Dr Eberhard Lisse, Feb 26, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.