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

Hi Monks, lets say i have a line

write("thepath",8'h44,status,"info")

how do i read the parts of above line into different perl variables

like
$a = "thepath"; $b = 8'h44; $c = status; $d = "info" and $e = write

Replies are listed 'Best First'.
Re: how to read words in a line into separate variables
by hippo (Archbishop) on May 27, 2015 at 09:51 UTC

    Here's the split way:

    ($e, $a, $b, $c, $d) = split (/[(),]/, q{write("thepath",8'h44,status,"info")});

    But you could also use capture groups or a CSV parser or whatever is most appropriate for your data population rather than this single given example.

Re: how to read words in a line into separate variables
by kroach (Pilgrim) on May 27, 2015 at 20:20 UTC

    You can use a regular expression with capture groups in list context:

    my $string = q{write("thepath",8'h44,status,"info")}; my ($e, $a, $b, $c, $d) = $string =~ /\A(.+?)\("(.+?)",(.+?),(.+?),"(. ++?)"\)\Z/;

    I don't know the exact format of your data, so I used non-greedy anything match (.+?). It's better to adjust the expression to the format as strictly as possible, though.