in reply to how to parse to get only the replica names in an array

You can try something like:

use strict; use warnings; my @replicas; while (<DATA>){ next if (! /^\d+/); /replica\s+"(.+)"/; push @replicas,$1; } __DATA__ 2005-04-01 root replica "ml_v_dialer" 2004-06-22 root replica "pu_v_dialer" 2006-02-11 ccvob01 replica "rd_v_dialer" "v_dialer replica for Redmond" 2003-11-25 root replica "v_dialer_drcalvin"

Update: Another possibility (if the file to process is not too big):

use strict; use warnings; { local $/; my @replica = <DATA> =~ /replica\s+"(.+)"/g; }

citromatik

Replies are listed 'Best First'.
Re^2: how to parse to get only the replica names in an array
by naikonta (Curate) on Aug 08, 2007 at 10:31 UTC
    The third would return rd_v_dialer" "v_dialer replica for Redmond instead of the wanted rd_v_dialer. You need the non-greedy quantifier with "?" to match the nearest closing '"' (as little as possible). So it should be /replica\s+"(.+?)"/;.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Hmmm, Are you sure? the dot "." matches everything but "\n", so it will not expand through newlines (see perlretut):

      use strict; use warnings; use Data::Dumper; { local $/; my @replica = <DATA> =~ /replica\s+"(.+)"/g; print Dumper \@replica; } __DATA__ 2005-04-01 root replica "ml_v_dialer" 2004-06-22 root replica "pu_v_dialer" 2006-02-11 ccvob01 replica "rd_v_dialer" "v_dialer replica for Redmond" 2003-11-25 root replica "v_dialer_drcalvin"

      Outputs:

      $VAR1 = [ 'ml_v_dialer', 'pu_v_dialer', 'rd_v_dialer', 'v_dialer_drcalvin' ];

      And:

      use strict; use warnings; use Data::Dumper; my @replica; while (<DATA>){ next if (! /^\d+/); /replica\s+"(.+)"/; push @replica,$1; } print Dumper \@replica; __DATA__ 2005-04-01 root replica "ml_v_dialer" 2004-06-22 root replica "pu_v_dialer" 2006-02-11 ccvob01 replica "rd_v_dialer" "v_dialer replica for Redmond" 2003-11-25 root replica "v_dialer_drcalvin"

      Prints the same:

      $VAR1 = [ 'ml_v_dialer', 'pu_v_dialer', 'rd_v_dialer', 'v_dialer_drcalvin' ];

      citromatik

        Oh my, we have different interpretation about the input lines :-) You have five lines, I only have four because I thought that the "v_dialer replica for Redmond" line was lined up with "rd_v_dialer". Of course, your codes are just fine in your situation (with five lines of input). Now I know what that next... line is for. And yes, I know about "." and "\n" and "/s".

        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!