• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

strip and keep the first five data inputs

Hi all

I would like to strip and keep the first five data inputs. The array length may vary then sample.

array Input: |data1|data2|data3|data4|data5|data6|data7|data8|data9|data10|data11

Result: data1|data2|data3|data4|data5

0
blnukem
Asked:
blnukem
  • 3
  • 2
2 Solutions
 
ozoCommented:
perl -lpe 's/\|*(([^|]*\|){4}[^|]*).*/$1/'
0
 
midrisiCommented:
Below code snippet will do what you like.This array can be expanded also.

@array=("data1","data2","data3","data4","data5","data6","data7","data8","data9","data10");

foreach $res (0..4)
{
        push(@result,$array[$res]);
}
print @result;
0
 
ozoCommented:
@result=@array[0..4];
0
 
midrisiCommented:
So modified code is

@array=("data1","data2","data3","data4","data5","data6","data7","data8","data9","data10");

@result=@array[0..4];

print @result;
0
 
ozoCommented:
I thought you wanted to print "data1|data2|data3|data4|data5" given input "|data1|data2|data3|data4|data5|data6|data7|data8|data9|data10|data11"
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now