Perl - Need help on splitting string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Need help on splitting string
# 1  
Old 03-06-2009
Perl - Need help on splitting string

Let's say I have a very long string with no spaces but just words stored in $very_long_string.

$very_long_string = "aaaaaaaaaaabbbbbbbbbbbccccccccccccdddddddddddd";

I can do this to split the string into 1 character each and store them in an array:
@myArray = split(//, $very_long_string);

If instead I want to split $very_long_string into 2 characters, how would I do that? 3 characters? Or more?

Last edited by teiji; 03-06-2009 at 11:49 PM..
# 2  
Old 03-07-2009
You don't want to use the split() function unless (as you are doing) you want to split all the characters (no delimiter) or there is a well defined delimiter to split on. What you can do is use a matching regexp and a caturing group () to populate the array per your requirements:

Code:
$very_long_string = "aaaaaaaaaaabbbbbbbbbbbccccccccccccdddddddddddd";
@array = $very_long_string =~ /(.{1,2})/g;
print "$_\n" for @array;

where '.' is any character and {1,2} is a quantifier meaning 1 minimum, 2 maximum, character per matched group. The 'g' on the end tells the regexp to perform the match over the entire string. Replace 2 with 3 to match in group of three (or less).
# 3  
Old 03-07-2009
Thank you.

Last edited by teiji; 03-07-2009 at 01:27 PM..
# 4  
Old 03-09-2009
Code:
$var="abcdefghijklmnopqrstuvw";
$var=reverse $var;
@arr=split(/(?=(?:..)+$)/,$var);
@arr=reverse @arr;
for($i=0;$i<=$#arr;$i++){
	$arr[$i]=reverse $arr[$i];
}
print join "-",@arr;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl, splitting out specific parts of the string

Hi there, I have an output from a command like this # ypcat -k netgroup.byuser| grep steven steven.* users_main,users_sysadmin,users_global,users_backup_team and wanted to pull the 'users' netgroups returned into a perl array, that will look like this users_main... (2 Replies)
Discussion started by: rethink
2 Replies

2. Shell Programming and Scripting

Splitting string with awk

Input: Debris Linux is a minimalist, desktop-oriented distribution and live CD based on Ubuntu. It includes the GNOME desktop and a small set of popular desktop applications, such as GNOME Office, Firefox web browser, Pidgin instant messenger, and ufw firewall manager. Debris Linux ships... (5 Replies)
Discussion started by: cola
5 Replies

3. UNIX for Dummies Questions & Answers

Help with splitting a string..

Hello I need help with the following. I have strings like #if defined(__def1__) #if defined(__def1__) || defined(__def2__) #if defined(__def1__) && defined(__def2__) && defined(__def3__). #if defined(__def1__) || defined(__def2__) || defined(__def3__). I need to print what is there in... (4 Replies)
Discussion started by: srk
4 Replies

4. Shell Programming and Scripting

splitting words from a string

Hi, I have a string like this in a file, I want to retrive the words separated by comma's in 3 variables. like How do i get that.plz advice (2 Replies)
Discussion started by: suresh_kb211
2 Replies

5. UNIX for Dummies Questions & Answers

Splitting a string and putting another string in the middle?

Hello all! I'm trying to put together a small script that will take in a file name and attach a datestamp to the end of it (but before the file type extension). To illustrate... Before: filename.txt anotherfilename.txt After: filename_20090724.txt anotherfilename_20090724.txt ... (7 Replies)
Discussion started by: jisoo411
7 Replies

6. Programming

Is this string splitting function OK?

Hello, I am recently working on an application that sends large strings accross a network very often. These then need to be broken up first with '!' and then with ','. My current function (below) works fine for this when not too much data is being sent across the network but segfaults when a... (4 Replies)
Discussion started by: kpedersen
4 Replies

7. Shell Programming and Scripting

Splitting a string with awk

Hi all, I want to split a string in awk and treat each component seperatley. I know i can use: split ("hi all", a, " ") to put each delimited component into array a. However when i want to do this with just a string of chars it does not work split ("hi", a, ""); print a; prints... (6 Replies)
Discussion started by: pxy2d1
6 Replies

8. Shell Programming and Scripting

Splitting a string

Hi, I am new to shell scripting, Can any body suggest me how I can split a string with a delimiter as whitespace into words and store into a array. I have read a line from file, now I want to split the line into words and store in a array for further use. eg : the sky is blue want... (3 Replies)
Discussion started by: smk
3 Replies

9. Shell Programming and Scripting

string splitting

Hi, I have a string like 'xyz' and i want to to split the above string into letters for comparision purpose. any idea pls. cheers RRK. (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

10. UNIX for Dummies Questions & Answers

splitting a string in unix

i need to split a line using a delimiter, and store it into a array :( (2 Replies)
Discussion started by: lmadhuri
2 Replies
Login or Register to Ask a Question