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

$file = '/root/db.dns.com'; # Name the file open(INFO, $file); # Open the file @lines = <INFO>; # Read it into an array close(INFO); # Close the file #print @lines; # Print the array foreach $i (@lines) { @a = split(/ /, $i); print @a[0] . "\n"; }

I need some help with regex's . The file that is opening looks like this
asdfrbrn IN A 206.23.12.5 a234rn IN A 206.23.12.5
I need to get each item into a array. I don't know how many spaces there are between each word. I also need to make sure that the first item starts with a letter a-z A-Z. Can I do this with perl?

Replies are listed 'Best First'.
Re: split issue
by jfroebe (Parson) on Jan 02, 2005 at 01:25 UTC

    The following split should work nicely for you:

    @a = split( /\s+/, $i) if ($i =~ /^[a-z]/i);

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

      is "IN A" one or two items?
      if the first one is the case youŽll need to change /\s+/ to /\s\s+/ or /\s{2,}/.
Re: split issue
by Mr. Muskrat (Canon) on Jan 02, 2005 at 18:31 UTC

    It looks like DNS::ZoneParse would be better suited for this than a simple split.

Re: split issue
by Zaxo (Archbishop) on Jan 02, 2005 at 07:30 UTC

    Looks like a domain record file. You already have each line in an array with @lines = <INFO>;

    You can't just split on space, since the "IN A" bits are not constant for all records. To be really accurate about this you need to write a parser for zone files, maybe with Parse::RecDescent, or maybe relying on some local utility. As it is you may get by with,

    /^\s*(\w+)\s+IN A\s+(.*)$/ and push @array, [$1,$2];

    After Compline,
    Zaxo

Re: split issue
by CountZero (Bishop) on Jan 02, 2005 at 09:24 UTC
    As for your second question (make sure the entry starts with a-zA-Z):
    foreach $i (@lines) { next unless $i=~/^[a-zA-Z]/; # your script here }

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

A reply falls below the community's threshold of quality. You may see it by logging in.