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

Hi, i'm trying to extract names from a string which are separated by dashes. unfortunately perl extracts only the first and then every second one.

$transcript =~ s/\w?/-$1/; #add leading dash $transcript =~ s/ //g; while ($transcript=~/-(.*?)-/msig) { push @analysts, $1; } print "$_\n" for @analysts;

can somebody help? thank you in advance!

Replies are listed 'Best First'.
Re: extracting names between symbols
by NetWallah (Canon) on Nov 18, 2009 at 01:28 UTC
    Sample data please.

    Here is code based on my assumptions of what you need:

    my $transcript=q|tom t-Dick -Harry|; my @analysts= $transcript=~/([^-]+)-?/g; print qq|=$_=\n| for @analysts;
    Output:
    =tom t= =Dick = =Harry=
    The RE can be tightentd to ignore leading/trailing spaces.:
    my @a= $transcript=~/\s*([^-]+\w)\s*-?/g
    This deletes the space after =Dick.

         Theory is when you know something, but it doesn't work.
        Practice is when something works, but you don't know why it works.
        Programmers combine Theory and Practice: Nothing works and they don't know why.         -Anonymous

      Thanks to both of you. Sorry for not presenting all the necessary infos. The second solution is perfect, because i need an array to work further with.

        Glad to hear that you can proceed further!

        The solution from NetWallah looks fine to me.

        I added a line to make it clear that the foreach() in my code does use an @variable. And I showed the idioms for deleting whitespace before and after a "line". Anyway, very happy that you can proceed further!

        #!/usr/bin/perl -w use strict; my $line = "this- is-aline - one- two "; my @array = split (/-/,$line); foreach (@array) { s/\s+$//g; #throw away tailing whitespace s/^\s+//g; #throw away leading whitespace print "$_\n"; } __END__ PRINTS: this is aline one two
Re: extracting names between symbols
by Marshall (Canon) on Nov 18, 2009 at 00:53 UTC
    Please show desired output from a simple input case. I worked from your problems statement of:"i'm trying to extract names from a string which are separated by dashes" rather than your code. I think split() is better here than regex().
    #!/usr/bin/perl -w use strict; my $line = "this-is-aline-one-two"; foreach (split (/-/,$line)) { print "$_\n"; } __END__ PRINTS: this is aline one two