Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^4: Regex Doubt.

by Anonymous Monk
on Oct 16, 2012 at 03:21 UTC ( #999208=note: print w/replies, xml ) Need Help??

in reply to Re^3: Regex Doubt.
in thread Regex Doubt.

my $total_names = "Matthew,Thomas,Peter,Randy,George,Federick"; my $last_name = $1 if ($total_names =~ /(,[^,]+)$/ ); print "$last_name;

Thanks Monk.

Replies are listed 'Best First'.
Re^5: Regex Doubt.
by Athanasius (Chancellor) on Oct 16, 2012 at 03:40 UTC

    The error says:

    Can't find string terminator '"' anywhere before EOF

    and your code has:

    print "$last_name; # ^

    Nothing to do with the regex.

    Actually, the regex would be better not capturing the comma, and there is no need to quote the variable at all:

    my $total_names = "Matthew,Thomas,Peter,Randy,George,Federick"; $total_names =~ /,([^,]+)$/; print $1 if $1;


    my $total_names = "Matthew,Thomas,Peter,Randy,George,Federick"; my ($last_name) = $total_names =~ /,([^,]+)$/; print $last_name if $last_name;

    Update: If your teacher does allow you to use split, you’ll get the last substring by subscripting with -1:

    my $last_name = ( split /,/, $total_names ) [ -1 ] ;

    (A [ 0 ] subscript gives you the first substring.)

    Hope that helps,

    Athanasius <°(((><contra mundum

Re^5: Regex Doubt.
by Anonymous Monk on Oct 16, 2012 at 03:42 UTC
    print "$last_name;
          |          \--missing quote
    first quote

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999208]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2018-02-21 23:56 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.