How to get array item directly from split result?

H

Huajian Luo

Hi there,

I'm now parse a output from a command which need use split to
split each line to array and get the fields like the following
pseudo code.

for my $line (@lines) {
if ($line =~ /mb_setno/) {
$line =~ s/\s//;
my @fields = split(/\s+/, $line);
my $100th_fld = $fields[99];
}
}

Cause the fields is too much, I wanna skip the @fields by just
my $100th_fld = split(/\s+/, $line){99}; or sth like that, but
it didn't work, does any guys tried this or still any hints on
this.
 
G

Gunnar Hjalmarsson

Huajian said:
I'm now parse a output from a command which need use split to
split each line to array and get the fields like the following
pseudo code.

for my $line (@lines) {
if ($line =~ /mb_setno/) {
$line =~ s/\s//;
my @fields = split(/\s+/, $line);
my $100th_fld = $fields[99];
}
}

Cause the fields is too much, I wanna skip the @fields by just
my $100th_fld = split(/\s+/, $line){99}; or sth like that, but
it didn't work, does any guys tried this or still any hints on
this.

This gives you the 100th field:

( split /\s+/, $line )[99]

Is that what you mean?
 
J

John W. Krahn

Huajian said:
Hi there,

I'm now parse a output from a command which need use split to
split each line to array and get the fields like the following
pseudo code.

for my $line (@lines) {
if ($line =~ /mb_setno/) {
$line =~ s/\s//;
my @fields = split(/\s+/, $line);
my $100th_fld = $fields[99];
}
}

Cause the fields is too much, I wanna skip the @fields by just
my $100th_fld = split(/\s+/, $line){99};

That should be:

for my $line ( @lines ) {
if ( $line =~ /mb_setno/ ) {
my $100th_fld = ( split ' ', $line )[ 99 ];
}
}


Or if you want to save some typing:

for ( @lines ) {
if ( /mb_setno/ ) {
my $100th_fld = ( split )[ 99 ];
}
}




John
 
H

Huajian Luo

John W. Krahn said:
That should be:

for my $line ( @lines ) {
if ( $line =~ /mb_setno/ ) {
my $100th_fld = ( split ' ', $line )[ 99 ];
}
}


Or if you want to save some typing:

for ( @lines ) {
if ( /mb_setno/ ) {
my $100th_fld = ( split )[ 99 ];
}
}

Yes, that's what I'm looking for and I just wanna save typing
and machine memory, Thanks everyone for the response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top