Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Arrays

Status
Not open for further replies.
Oct 14, 2009
5
AR
Hi. I'm beginner with JavaScript. I need to do the following:


<textarea example>

text1/tomy/text2/
text1/john/text2/
text1/dany/text2/
text1/mike/text2/

</textarea>



<Problem>

I need say:

"each line of textarea have 3 parts":

Part1[0]=text1/;
Part2[1]="whatever is on the second part"/;
Part3[2]=text2/;

</problem>



now write on textarea [1]+[0]+[2] for each line:

tomy/text1/text2/
john/text1/text2/
dany/text1/text2/
mike/text1/text2/


or "[0]+[2]+[1]":

text1/text2/tomy/
text1/text2/john/
text1/text2/dany/
text1/text2/mike/


or "[0]+[1]+[1]+[1]":

text1/tomy/tomy/tomy/
text1/john/john/john/
text1/dany/dany/dany/
text1/mike/mike/mike/

 
the problem is in:

<Problem>text</problem>

I do not know how to create a variable that says
"whatever is on the second part of each line
 
Hi

Here is a starting point :
JavaScript:
[gray]// put the original text into variable str[/gray]
[b]var[/b] result[teal]=[/teal][green][i]''[/i][/green][teal];[/teal]
[b]var[/b] line[teal]=[/teal]str[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][fuchsia]/\r?\n/[/fuchsia][teal]);[/teal]
[b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]line[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
  [b]var[/b] part[teal]=[/teal]line[teal][[/teal]i[teal]].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]'/'[/i][/green][teal]);[/teal]
  result[teal]+=[/teal]part[teal][[/teal][purple]1[/purple][teal]]+[/teal][green][i]'/'[/i][/green][teal]+[/teal]part[teal][[/teal][purple]0[/purple][teal]]+[/teal][green][i]'/'[/i][/green][teal]+[/teal]part[teal][[/teal][purple]2[/purple][teal]]+[/teal][green][i]'/[/i][/green][lime][i]\n[/i][/lime][green][i]'[/i][/green][teal];[/teal]
[teal]}[/teal]
[gray]// the variable result contains the modified text[/gray]
Please note that Tek-Tips is not a code writing service.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top