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
    1. Advertisements

  2. Dr Eberhard Lisse

    Marc Girod Guest

    You don't mean...

    perl -le '
    print;
    @dn = qw(lisse.NA www.lisse.NA);
    for (@dn) {
    @f = /(\.)/g;
    print"$_: ", scalar @f;
    }'lisse.NA: 1
    www.lisse.NA: 2

    ....do you?
    Marc
     
    Marc Girod, Feb 25, 2013
    #2
    1. Advertisements

  3. Dr Eberhard Lisse

    Dave Saville Guest

    my $domain = 'a.b.com';
    my $dots = () = $domain =~ m{\.}g;
    print $dots;
     
    Dave Saville, Feb 25, 2013
    #3
  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
    string?
    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
    #4
  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 (host.name) and absolute,
    or fully-qualified, (host.name.example.org.) 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
    #5
  6. Thanks to all.

    The trailing dot I had already removed :)-O

    el

     
    Dr Eberhard Lisse, Feb 26, 2013
    #6
  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 = "tear.mssgmbh.com"; print $a =~ tr/.//, "\n";'
     
    Rainer Weikusat, Feb 26, 2013
    #7
  8. Thanks.

    el

     
    Dr Eberhard Lisse, Feb 26, 2013
    #8
    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.