You can index directly into the result of a function call:
$schema_username = ( split '=', $line )[1];
Note also that the first argument to split is a regex, not a string literal, even if you mean it to be. In particular, split '.', EXPR probably won't behave as you expect.
(In fact, split '\.', EXPR and split /\./, EXPR behave differently—I don't know why.
Oh, now I see. What I actually tested was split "\.", EXPR, and Perl regards "\." as a (pointless) escaping of ., so that it's read the same as "."—which, when interpreted as a regex, matches any single character. In split '\.', EXPR, no escaping is done, so everything's fine.) | [reply] [d/l] [select] |
>perl -wMstrict -le
"my $line = 'foo=bar=baz';
my (undef, $schema_username) = split /=/, $line;
print $schema_username;
"
bar
Update: Although it must be admitted that this approach would get a bit tedious if one were interested not in the second field but in the twenty-second! | [reply] [d/l] |
Although it must be admitted that this approach would get a bit tedious if one were interested not in the second field but in the twenty-second!
I hoped for a mad moment that
my ( ( undef ) x 21, $var ) = func;
or at least
( ( undef ) x 21, my $var ) = func;
would work—but they don't …. | [reply] [d/l] [select] |
| [reply] |
my $schema_username = ( split /=/, $line )[1];
or
my ($schema_username) = $line =~ /=(.*)/s;
By the way, the first arg of split is a regex. Don't fool yourself into thinking it matches literally by using quotes.
- ikegami
| [reply] [d/l] [select] |
What about if I need 3 of the elements, not just one?
Right now I'm using:
$line = "aa;bb;cc;dd;ee";
my @stuff = split(/;/, $line);
my $a = $stuff[0];
my $b = $stuff[1];
my $d = $stuff[3];
Is there a shortcut to this?
I'm actually parsing a csv and only need columns 1, 2 and 8...
Thanks! | [reply] [d/l] |
I'm actually parsing a csv and only need columns 1, 2 and 8
my ($a, $b, $c) = (split /;/, $line)[0,1,7];
| [reply] [d/l] |
Thanks! My brain is fried, I kept trying multiple [] like
[0][1][7] and [0],[1],[7]...
| [reply] [d/l] |