Perl split question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl split question
# 1  
Old 10-13-2008
Perl split question

hi, I have a seemingly really stupid question, but here goes!

What do you enter into split delimiter to seperate something like this

"December 12, 1995"

and get

December
12
1995

?

thanks
# 2  
Old 10-13-2008
A space " ".
# 3  
Old 10-13-2008
what about the space and the comma?
# 4  
Old 10-13-2008
Code:
$string = "December 12, 1995";
$string =~ s/,//g;

($month,$day,$year) = split(" ",$string);

print "$month -- $day -- $year\n";

# 5  
Old 10-13-2008
Use the \W character class to split the string. \W is the compliment of \w which is a-zA-Z0-9_

Code:
$string = "December 12, 1995";
($month,$day,$year) = split(/\W+/,$string);
print "$month\n$day\n$year";

# 6  
Old 10-14-2008
Code:
$str="December 12, 1995";
@arr=split("[ |,]",$str);
for($i=0;$i<=$#arr;$i++){
print $arr[$i],"\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split the string in perl

Hi All, How to split the string KAR_Celltick_Ban_GSMGW3 and want to pickup the third filed. Sometime the string may be "KAR_Celltick_Ban" like this Thanks in advance (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Perl Question - split function with csv file

Hi all, I have a csv file that appears as follows: ,2013/03/26,2012/12/26,4,1,"2017/09/26,5.75%","2017/09/26,1,2018/09/26,1,2019/09/26,1,2020/09/26,1,2021/09/26,1",,,2012/12/26,now when i use the split function like this: my @f = split/,/; the split function will split the data that is... (2 Replies)
Discussion started by: WongSifu
2 Replies

3. Shell Programming and Scripting

Perl split and join

Hi, I have tried the split and join functions but stuck with unexpected results. Any help appreciated. I pass multiple values at command line like perl test.pl -type java,xml. This works good for me but i am not sure how to print it in the required format. Here is the code i tried:... (4 Replies)
Discussion started by: nmattam
4 Replies

4. Programming

Perl Split Issue

Hello All - I am having trouble with the split command in perl. Here is what I am trying to do. 75|2455345|2455349|00:00:00|00:00:00|Once|0|Frank Vacation | | 5 I want to only print out the 8th column. So that would be "Frank Vacation" and "Power Down" I need to run this on a file that... (16 Replies)
Discussion started by: tarken
16 Replies

5. UNIX for Dummies Questions & Answers

File split question

I have a flat file in UNIX and I have to perform two tasks based on the below data. The data I have printed here is just sample the original data is too long. The position 110 to 111 (two digit value I have bolded the values) theygives the record type detail in the sample above the record types... (7 Replies)
Discussion started by: techsavvy007
7 Replies

6. Shell Programming and Scripting

Use split function in perl

Hello, if i have file like this: 010000890306932455804 05306977653873 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC30693599000 30971360000 ZZZZZZZZZZZZZZZZZZZZ202011302942311 010000890306946317387 05306977313623 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC306942190000 30971360000... (5 Replies)
Discussion started by: chriss_58
5 Replies

7. UNIX for Advanced & Expert Users

Split Command in Perl

Hi, I have to split a line of the form 1232423#asdf#124324#54534#dcfg#wert#rrftt#4567 into an array in perl. I am using @fields; @fields=split('#',$line); if($fields eq "1") But this is not working. By using the syntax, the statements in "if" are never executed. Please help.... (9 Replies)
Discussion started by: rochitsharma
9 Replies

8. Shell Programming and Scripting

split to array in perl

Collegues I have flat file in the following format. 137 (NNP Kerala) (NNP India) 92 (NN Rent) (NN Range) 70 (NNP Thiruvananthapuram) (NNP Kerala) 43 (NNP Tourist) (NNP Home) 40 (NNP Reserve) (NNP Now) 25 (SYM @) (NN hotelskerala) 25 (NNP Thiruvananthapuram-695001) (NNP Kerala) 23 (NN... (3 Replies)
Discussion started by: jaganadh
3 Replies

9. UNIX for Dummies Questions & Answers

Split and recombine question

Hi guys I would like to be able to split a large file into many smaller part. Then, these smaller files will be transfered onto a windows machine where they need to be recombined. I think tar files may be the best to do this. How can I tar a large file into many small tar files which can be... (1 Reply)
Discussion started by: white_raven0
1 Replies

10. Shell Programming and Scripting

split question perl

I am interested in 2 and 36th fields in this input file. I was wondering if there was a more efficeint way to do this. ($pt1,$bkup_name,$pt3,$pt4,$pt5,$pt6,$pt7,$pt8,$pt9, $pt10,$pt11,$pt12,$pt13,$pt14,$pt15,$pt16,$pt17, ... (7 Replies)
Discussion started by: reggiej
7 Replies
Login or Register to Ask a Question