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

@su = an array of output data in the following format (one line = 1 array element)

(there are spaces at the beginning of the lien as well)
|-aterm(2559,ddicks)---screen(2560)---screen(2561)---bash(2563)---su(1 +4303,root)---bash(14306)
i have the following code.
foreach (@su_procs) { print "\n$_\n"; $user eq split /.*\(/; print "$user\n"; }
all i am getting as $user is the number "2". what am i doing wrong? in the end i am going to want to get the first number and user and the number after su(

Replies are listed 'Best First'.
Re: learning: why doesn't this split work?
by davido (Cardinal) on Aug 25, 2003 at 23:50 UTC
    1. You're using split in scalar context, when you really mean to receive a list from it.

    2. Your .* might be matching greedily. Perhaps .*? would be better in this situation.

    3. You are telling split to split on a bunch of anything (except newlines) followed by an open parenthesis. Since .* is greedy, it's matching everything from the beginning of the line to the last left paranth. Split slurps up (and throws away) the delimeters (in this case, everything preceeding and including the last open parenthesis). Even if .* wasn't greedy, your definition might be causing -aterm( to become the first delimeter, and 2559,ddicks)---screen( to become the next delimeter, leaving nothing behind to return as the list of things between delimeters.

    4. You are using eq. Eq isn't an assignment operator, it's a comparative operator. You need to be using = instead.

    You should probably use a method involving regexp matches with capturing rather than split. And make sure to beware of greedy .*. But of course there is more than one way to do it. ;)

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: learning: why doesn't this split work?
by bobn (Chaplain) on Aug 25, 2003 at 22:41 UTC

    $user eq split /.*\(/; - beccause $user is a scalar variable, the output of the split command is the number of items into which it split $_.

    if you use  ( $var1, $var2, $var3 ) = split /.*\(/; then the first 3 elements of the list produced by split will be put in those variables.

    But I'm suspicous of the regex you're using in the split. Also, the input got mangled - try enclosing it in code tags or pre tags.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      $user does not force scalar context, the eq operator does.
Re: learning: why doesn't this split work?
by sgifford (Prior) on Aug 25, 2003 at 23:01 UTC

    What do you want $user to contain?

    Also, your assignment code isn't correct. = is used to assign a value to a variable; eq tests for string equality. I'm not sure how you end up with $user set to anything using eq...

Re: learning: why doesn't this split work?
by Anonymous Monk on Aug 26, 2003 at 00:12 UTC
    you want assignment, not an equality test.
    you want $user = split /.*\(/;
Re: learning: why doesn't this split work?
by Hayl_ (Acolyte) on Aug 26, 2003 at 02:14 UTC
    oops about the eq. the problem ended up being the missing () around the $user variable.