Solved

strip and keep the first five data inputs

Posted on 2007-03-26
5
239 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 129
remove duplicates from the csv file 13 115
unable to remove invisible characters with perl 2 89
read an xml file in perl 2 49
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

856 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