Solved

strip and keep the first five data inputs

Posted on 2007-03-26
5
238 Views
Last Modified: 2010-03-05
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
Comment
Question by:blnukem
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18794824
perl -lpe 's/\|*(([^|]*\|){4}[^|]*).*/$1/'
0
 
LVL 1

Accepted Solution

by:
midrisi earned 250 total points
ID: 18795104
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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 18795168
@result=@array[0..4];
0
 
LVL 1

Expert Comment

by:midrisi
ID: 18795362
So modified code is

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

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

print @result;
0
 
LVL 84

Expert Comment

by:ozo
ID: 18795416
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 128
Filtering a file to table 9 101
perl to convert excel to csv 3 215
read an xml file in perl 2 33
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question