in reply to Referencing a Backreference

Wouldn't something like this be a lot simpler?
my @ips = (); my @names = (); while (<DATA>) { chomp; my ($ip, $name) = split /\s+/, $_, 2; push @ips, $ip; push @names, $name; } use Data::Dumper; print Dumper(\@ips, \@names); __DATA__ 192.168.89.1 acmeorp.acme.com 192.168.31.3 ftp.acme.com 192.168.19.179 [Unknown]

gav^

Replies are listed 'Best First'.
Re: Re: Referencing a Backreference
by Juerd (Abbot) on Mar 14, 2002 at 16:49 UTC

    Wouldn't something like this be a lot simpler?

    So true, but it can be done even simpler!

    my (@ips, @names); while (<DATA>) { my ($ip, $name) = split; push @ips, $ip; push @names, $name; }
    split with no arguments is like split ' ', $_, which in turn has a special meaning because of the chr(32) string. I think you can asume ip adresses and hostnames do not contain whitespace...

    If order and duplicates are not very important, a hash would be really nice:
    my %hash = map split, <DATA>; my @ips = keys %hash; my @names = values %hash;

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      Not such a big deal here unless it's a huge log being parsed, but probably generally not the best generic scheme.

      --
      perl -pe "s/\b;([st])/'\1/mg"