[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

splitting a string on delimeter

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 41
Thread images: 6

File: 675b48cfe5ba.jpg (208KB, 1864x1864px) Image search: [Google]
675b48cfe5ba.jpg
208KB, 1864x1864px
how can i turn this:

A,a1,a2,a3
B,b1,b2


into this:

A,a1
A,a2
A,a3
B,b1
B,b2

using whatever tool works (bash/sed/python/excel...)
all the elements are strings (of variable length)
>>
>>60057903
Ask your professor.
>>
transform it into a list, and then break the list in half when you encounter the delimiter
>>
>>60057903
Sauce me
>>
>>60057946

i don't follow
>>
>>60057903
that's a hot bitch
>>
>>60057903
4chan.c
#include <stdio.h>
#include <string.h>

int main() {
char str[128], *p;

printf("In: ");
scanf("%s", str);

p = strtok(str, ",");
while (p != NULL) {
puts(p);
p = strtok(NULL, ",");
}

return 0;
}


gcc 4chan.c
>>
Photoshop tutorial.
Cut every string into a new layer.
Duplicate A and B as many times needed.
Rearrange everything.
Merge layers visible.
>>
You could do it in bash easily
>>
this is a problem for search engines
>>
>>60058127

i can split a string on a delimiter easily, but don't know how to duplicate the A and B across multiple rows
>>
>>60057903
perl -lne '@a=split /,/;$a=shift @a;print "$a,$_" for @a'
>>
File: has_u_red_it.jpg (3MB, 2448x3264px) Image search: [Google]
has_u_red_it.jpg
3MB, 2448x3264px
>>60058108
Are you a wizard™?
>>
Are you fucking retarded? Please keep stupid questions in /sqt/. Do your own homework.
>>
>>60058090

thanks, but it doesn't duplicate the first element. your code does this:

> In: A,a,b,c,d
A
a
b
c
d

I am trying to do this:

> In: A,a,b,c,d
A,a
A,b
A,c
A,d
>>
>>60058208

that works, thanks!
i don't know perl. is there a way to feed it a text file?
>>
>>60058208
I have no idea how the fuck this works but this is one of the most beautiful things I've seen.
>>
>>60058277
perl -lne '@a=split /,/;$a=shift @a;print "$a,$_" for @a' input.txt
>>
>>60058293
A game: pipe /dev/random into a tr command that that makes the output ascii, then try to determine what the resultant perl program would do.
>>
File: 1ccb5b63ed36f725590432467d7.jpg (719KB, 1362x2048px) Image search: [Google]
1ccb5b63ed36f725590432467d7.jpg
719KB, 1362x2048px
>>60058338

thank you perl wizard
>>
>>60058232
oh i missread OP

#include <stdio.h>
#include <string.h>

int main() {
char str[128], *p;

printf("In: ");
scanf("%s", str);

char a[32];
p = strtok(str, ",");
strcpy(a, p);
while ((p = strtok(NULL, ",")) != NULL)
printf("%s,%s\n", a, p);

return 0;
}
>>
File: 1ccbd69eb8c17191f12d09c3d6f3.jpg (700KB, 1363x2048px) Image search: [Google]
1ccbd69eb8c17191f12d09c3d6f3.jpg
700KB, 1363x2048px
>>60058433

sweeeet
>>
>>60057903
liek this?
$ cat input.txt 
A,a1,a2,a3
B,b1,b2,b3
$ awk -F "," '{i=1;while(i++<NF){printf("%s,%s\n", $1, $i)}}' input.txt
A,a1
A,a2
A,a3
B,b1
B,b2
B,b3
>>
Who the cum dump
>>
File: Alexis-Ren-5.jpg (97KB, 853x1280px) Image search: [Google]
Alexis-Ren-5.jpg
97KB, 853x1280px
>>
>>60058051
I'd like to split her string on my delimiter if you know what I mean.
>>
>>60057903
leanr how to use your libraries
>>
>>60058090
Not thread safe
>>
>>60059469
Why in gods name would you make this thread safe?
>>
public static void splitWithDelimeters (String s1, String s2) {
String[] splitS1 = s1.split(",");
String[] splitS2 = s2.split(",");

for (int i = 1; i < splitS1.length; i++) {
System.out.println(splitS1[0] + "," + splitS1[i]);
}

for (int i = 1; i < splitS2.length; i++) {
System.out.println(splitS2[0] + "," + splitS2[i]);
}
}


Output:
A,a1
A,a2
A,a3
B,b1
B,b2
>>
>>60057903
It's probably explained in your textbook you lazy piece of shit.
>>
>>60057903
Awk would make that so fucking easy
>>
File: 1463869786995-1.jpg (311KB, 1416x2500px) Image search: [Google]
1463869786995-1.jpg
311KB, 1416x2500px
>>60058826

UNIX FTW!
>>
>>60058208
>perl -lne '@a=split /,/;$a=shift @a;print "$a,$_" for @a'
teach me how to specify the input feed for this so I can like you more
>>
>>60061647

>>60058338
>>
When I try to golf it
perl -F',' -lE'say"$F[0],$F[$_]"for 1..$#F' input.txt
>>
I'm writing my first few shit programs in C.

It's printing text using printf, but i want the text to slowly appear on the screen for dramatic effect. How do I do this without making a printf and delay for every character?
>>
>>60063481
You can't. But what you could do is make the code appear a little neater by storing the entire string in a char* array, then using a while() loop to loop through all characters in the array.

So something like this:
char* str = "hello";
char* c = str;
while(*c != '\0'){
printf("%c");
usleep(DELAY);
c++;
}

Just don't use it with arbitrary input, the string has to be null-terminated. And obviously you'll have to look into which type of delay function fits your intentions best.
>>
>>60058293
$ perl -MO=Deparse -lne '@a=split /,/;$a=shift @a;print "$a,$_" for @a' g_zk.txt 
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
@a = split(/,/, $_, 0);
$a = shift @a;
print "$a,$_" foreach (@a);
}
>>
>>60057903
data = open("temp", "r").read()
for line in data.split("\n"):
line = line.split(",")
for x in line[1:]:
print(line[0] + "," + x)


Don't they teach this shit in middle school now?
>>
>>60060816
gnu's not unix
Thread posts: 41
Thread images: 6


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.