473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

to split the string

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
18 3087
numberwhun
3,509 Expert Mod 2GB
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
Arepi
62
I had also this problem and I get answer see:
http://bytes.com/forum/thread845485.html
Oct 25 '08 #3
KevinADC
4,059 Expert 2GB
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
3,509 Expert Mod 2GB
Is this school work of some kind?
Yeah, that is my suspicion and the reason I did not provide code. :-)
Oct 25 '08 #5
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
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
4,059 Expert 2GB
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
971 Expert 512MB
dheenaalex,

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

Thank You,

Kevin
Moderator
Oct 25 '08 #9
numberwhun
3,509 Expert Mod 2GB
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
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
971 Expert 512MB
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
4,059 Expert 2GB
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
410 Expert 256MB
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
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
410 Expert 256MB

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
4,059 Expert 2GB
Well, hopefully he doesn't claim to have written any of that code when he submits it for grading.
Oct 29 '08 #17
Icecrack
174 Expert 100+
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
Hi Nithinpes,

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

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

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

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
4
by: Crirus | last post by:
There is a function somewhere to split a string with multiple tokens at a time? Say I have this: aaaa#bbbbb*ccccc$dddd I whould like to split it so the result whould be aaaa bbb
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.