By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,775 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,775 IT Pros & Developers. It's quick & easy.

to split the string

P: 13
I am having a String as "first" second third "fourth" "fifth" "sixth seventh" eigth

I want to spilt it as follows.

first
second
third
fourth
fifth
sixth seventh
eigth

please can any one help me regarding this?
Oct 25 '08 #1
Share this Question
Share on Google+
18 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
This is actually something that is very easy using an array and the split() function. Why don't you give it a try and if you get stuck, post your code here (enclosed in code tags) and we will nudge you in the right direction.

Regards,

Jeff
Oct 25 '08 #2

P: 62
I had also this problem and I get answer see:
http://bytes.com/forum/thread845485.html
Oct 25 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
I am having a String as "first" second third "fourth" "fifth" "sixth seventh" eigth

I want to spilt it as follows.

first
second
third
fourth
fifth
sixth seventh
eigth

please can any one help me regarding this?
Is this school work of some kind? Its a classic lesson on splitting strings (or parsing strings) using a delimiter that is embedded inside the string inside of quotes. In your case the delimiter appears to be a space instead of a comma like you would find in a CSV file.
Oct 25 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
Is this school work of some kind?
Yeah, that is my suspicion and the reason I did not provide code. :-)
Oct 25 '08 #5

P: 13
This is actually something that is very easy using an array and the split() function. Why don't you give it a try and if you get stuck, post your code here (enclosed in code tags) and we will nudge you in the right direction.

Regards,

Jeff
Hello Jeff,

yes its easy to split a string with single seperator in split function, but here i want to split it as i shown already, whats the output i need..

can you?
Oct 25 '08 #6

P: 13
Yeah, that is my suspicion and the reason I did not provide code. :-)
hello mr...

I want the exact out put from that string what i mentioned already...

In my string what i am having is, some values with double quotes and some is without double quotes, but i need to split the both of them. In split function we can split using single delimiter, but how can its possible for my output..

So i entered in to forum..

can you guide me regarding this?
Oct 25 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
You have not answered my question concerning school work so now I am convinced it is school work. If you post the code you have written and tried to solve your requirements I will consider helping. But I am not going to do your school work for you or even guide you until I see some effort on your part.

Regards,
Kevin
Oct 25 '08 #8

eWish
Expert 100+
P: 971
dheenaalex,

I have deleted your duplicate posts. Please do not make duplicate posts.

Thank You,

Kevin
Moderator
Oct 25 '08 #9

numberwhun
Expert Mod 2.5K+
P: 3,503
hello mr...

I want the exact out put from that string what i mentioned already...

In my string what i am having is, some values with double quotes and some is without double quotes, but i need to split the both of them. In split function we can split using single delimiter, but how can its possible for my output..

So i entered in to forum..

can you guide me regarding this?
If you read the split() functions page, it takes a regular expression where you specify the delimiter. Using that, you would specify the regular expresion equivelant of a space and that would be your delimeter. You populate that into an array and print it how you want it formatted.

Why not try it and write the code and see if you can get it outputting the way that you want. I just did this in like a minute and it works fine.

Being that I agree with KevinADC that this is obviously school work, we are not going to just give you the answer. In case you haven't read them, our site Posting Guidelines strictly forbid the posting of homework questions in order to obtain the answers. That is why we are prodding you in the right direction but not giving you what you want.... the answer to your homework.

What I am saying is do the work and learn. We aren't going to just do it for you and there is no use in getting upset over it.

Regards,

Jeff
Oct 26 '08 #10

P: 13
You have not answered my question concerning school work so now I am convinced it is school work. If you post the code you have written and tried to solve your requirements I will consider helping. But I am not going to do your school work for you or even guide you until I see some effort on your part.

Regards,
Kevin
Hi Kevin,

Please see my Input and Output clearly.. and also i want the output in the same order of string..

I am having a String as "first" second third "fourth" "fifth" "sixth seventh" eigth.


$str=qq"first" second third "fourth" "fifth" "sixth seventh" eigth);


@str1=split(/ /,$str); # this is the split function.. i know.. but i want the result as shown below.

first
second
third
fourth
fifth
sixth seventh
eigth

what i did here is i want to split the strings which is in space as well as fully from double quotes as a single string.. How can i do that..?

And my full code is as follows.. it was done to do the file writing in recursion..

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. #print "Give the Input File Name \n";
  3. #$filein=<STDIN>;
  4. #print "Give the Output File Name \n";
  5. #$fileout=<STDIN>;
  6.  
  7. $fileout="output.txt";
  8. $filein="first.txt";
  9.  
  10. open(DEST, ">$fileout");
  11.  
  12. $ln=1;
  13.  
  14.  
  15. func1($filein);
  16.  
  17. sub func1{
  18.  
  19.   $file=$filein;
  20.  
  21.   local $i=1;
  22.   local $valin=0;
  23.  
  24.   open(MYTEXT, "$file");
  25.  
  26.   local @contents=<MYTEXT>;
  27.   local    @valval=@val;
  28.  
  29.   foreach $line  (@contents){
  30.  
  31.  
  32.  
  33.     if($line =~ /Include/){
  34.     func($line);
  35.  
  36.     }
  37.  
  38.     elsif($line =~ /\$/){
  39.  
  40.  
  41.         $line =~ tr/[\$]/d/;
  42.  
  43.         $old="d$i";
  44.         print "$old \n";
  45.  
  46.  
  47.  
  48.          if($valval[$valin] =~ /"/){
  49.  
  50.              $valval[$valin]=~ tr/["]/ /;
  51.              $new=$valval[$valin];
  52.  
  53.          } else {
  54.  
  55.              $new=$valval[$valin];
  56.  
  57.          }
  58.  
  59.         $line =~ s/$old/$new/;
  60.         print "$line\n";
  61.         $i++;
  62.         $valin++;
  63.  
  64.         print DEST "Line $ln $line\n";
  65.         $ln++;
  66.  
  67.  
  68.     }
  69.  
  70.  
  71.     else {
  72.  
  73.  
  74.         print DEST "Line $ln $line\n";
  75.         $ln++;
  76.          }
  77.  
  78.  
  79.  
  80.   }
  81.  
  82.  
  83. }
  84.  
  85. sub func{
  86.  
  87.     @str1 = split(/ /,$line);
  88.  
  89.     foreach $cc (@str1){
  90.  
  91.     if($cc =~ /.txt/){
  92.  
  93.                  $cc =~ tr/["]/ /;
  94.                 $filein=$cc;
  95.  
  96.             }
  97.         }
  98.  
  99.     @str=split(/" "?/,$line);
  100.  
  101.     $len=$#str;
  102.  
  103.  
  104.  
  105.  
  106.             if($len>0){
  107.  
  108.             @val=@str[1..$len];
  109.  
  110.  
  111.  
  112.  
  113.         }
  114.  
  115.  
  116.         func1($filein,@val);    
  117.  
  118. }
  119.  
  120. close(DEST);
  121. close(MYTEXT);
Oct 26 '08 #11

eWish
Expert 100+
P: 971
dheenaalex,

This is a Formal Warning about making DOUBLE POST'S with your replies and/or questions. I again have deleted your others posts that were duplicates. Also, the code tags are required when posting code here. The code tags wok like so.

[CODE]...your code goes here....[/CODE]

Please make these adjustments to your posts. Please read our Posting Guidelines before making additional posts.

Thank You,

Kevin
Moderator
Oct 26 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
dheenaalex,

I am not trying to be nor do I want to be rude to you. But the code you posted clearly shows you have no idea what you are doing and have yet to understand even some very basic concepts and how some functions work, and etc.

What you are attempting to do is actually not all that easy and requires a good understanding of regular expressions. You don't yet understand how the much simpler tr/// operator works.

The obvious way to go about this is to split the string using the space between the words as the split delimiter. Then you would loop through the list that "split" returns and check if each word started with a double-quote and ended with a double-quote, or started with a double-quote or ended with a double-quote. The words with no qoutes will be the fall-through condition.

Ignoring all that code you posted because it really is so bad that it needs to be tossed and forgotten lest you remember any of it, and using your sample string here is a way that I think should be clear or that you can look up some of the syntax used in just about any perl reference book or online resource and understand it:

Expand|Select|Wrap|Line Numbers
  1. $string = '"first" second third "fourth" "fifth" "sixth seventh" eigth';
  2. @list = split/\s+/,$string;
  3. foreach my $w (@list) {
  4.    if ($w =~ /^"([^"]+)"$/) { # starts and ends with double-quotes
  5.       print "$1\n";
  6.    }
  7.    elsif ($w =~ /^"([^"]+)$/) {  # only starts with a double quote
  8.       print "$1 ";
  9.    }
  10.    elsif ($w =~ /^([^"]+)"$/) { # only ends with a double-quote
  11.       print "$1\n";
  12.    }
  13.    else { # no quotes at all (fall-through condition)
  14.       print "$w\n";
  15.    }
  16. }
  17.  
Sample data often does not reflect real data very well and the above will not work for something like three words inside of double quotes:

"word word word"

And there are probably other exceptions.

Now all you need to do is add your file I/O operations to the above code to read the data in from a file and print it out to another file.

There is a better and more robust solution to this type of problem but I think the code would be too advanced for you to understand at this point judging by the code you posted. Even the above is stretching it for you if this is school work. Any good teacher will be very suspicious that you wrote this code.
Oct 27 '08 #13

nithinpes
Expert 100+
P: 410
Hi Kevin,

Please see my Input and Output clearly.. and also i want the output in the same order of string..

I am having a String as "first" second third "fourth" "fifth" "sixth seventh" eigth.


$str=qq"first" second third "fourth" "fifth" "sixth seventh" eigth);


@str1=split(/ /,$str); # this is the split function.. i know.. but i want the result as shown below.

first
second
third
fourth
fifth
sixth seventh
eigth
One approach is to replace space inside double-qutes with any other character. Split the string across space and later replace the newly introduced character to space.
e.g "sixth seventh" to "sixth+seventh"
Choose a delimiter character which doesn't occur in the data.
Expand|Select|Wrap|Line Numbers
  1. $str=qq("first" second third "fourth" "fifth" "sixth seventh")
  2. $str=~s/("\w+)\s(\w+")/$1+$2/g;  ##inserting '+'
  3. @str1= split (/\s+/,$str);
  4. $_ =~ s/("\w+)\+(\w+")/$1 $2/ foreach(@str1) ; ## reverting back
  5.  
  6. print "$_\n" foreach(@str1);
  7.  

PS : Didn't notice Kevin's reply above(Page refresh issue ;)).
Oct 27 '08 #14

P: 13
Hi Nithinpes,

Thanks for your code..

But my string may be like this also..

$str=qq("first" second third "fourth" "fifth" "sixth seventh eigth .....nth words")

this code will work for only two words inside the double quotes..

could u please guide me regarding this,,..

Thanks and Regards,
Dheenaalex


One approach is to replace space inside double-qutes with any other character. Split the string across space and later replace the newly introduced character to space.
e.g "sixth seventh" to "sixth+seventh"
Choose a delimiter character which doesn't occur in the data.
Expand|Select|Wrap|Line Numbers
  1. $str=qq("first" second third "fourth" "fifth" "sixth seventh")
  2. $str=~s/("\w+)\s(\w+")/$1+$2/g;  ##inserting '+'
  3. @str1= split (/\s+/,$str);
  4. $_ =~ s/("\w+)\+(\w+")/$1 $2/ foreach(@str1) ; ## reverting back
  5.  
  6. print "$_\n" foreach(@str1);
  7.  

PS : Didn't notice Kevin's reply above(Page refresh issue ;)).
Oct 29 '08 #15

nithinpes
Expert 100+
P: 410

Hi Nithinpes,

Thanks for your code..

But my string may be like this also..

$str=qq("first" second third "fourth" "fifth" "sixth seventh eigth .....nth words")

this code will work for only two words inside the double quotes..

could u please guide me regarding this,,..

Thanks and Regards,
Dheenaalex
The following code will work for any number of words between quotes.
Expand|Select|Wrap|Line Numbers
  1. $str=qq("first" second third "fourth" "fifth" "sixth seventh eighth ninth") ;
  2. while($str=~/"([^"]+)"/g) {push @quoted,$1;} # fetch quoted words
  3. foreach(@quoted){
  4. $word=$new=$_;
  5. $new=~s/\s/+/g;  ##inserting '+' 
  6. $str=~s/$word/$new/;
  7. }
  8. @str1= split (/\s+/,$str); 
  9. $_ =~ s/\+/ /g foreach(@str1) ; ## reverting back 
  10.  
  11. print "$_\n" foreach(@str1); 
  12.  
Oct 29 '08 #16

KevinADC
Expert 2.5K+
P: 4,059
Well, hopefully he doesn't claim to have written any of that code when he submits it for grading.
Oct 29 '08 #17

Icecrack
Expert 100+
P: 174
Well, hopefully he doesn't claim to have written any of that code when he submits it for grading.

i agree, when i first started out i don't think i could of done that in 11 lines of code.
Oct 30 '08 #18

P: 13
Hi Nithinpes,

Thousands of thanks for you.. you are too smart.

Thanks and Rregards,
Dheenaalex.
Oct 30 '08 #19

Post your reply

Sign in to post your reply or Sign up for a free account.