Re: Manipulating file content
On Wed, Jun 28, 2000 at 02:37:33PM -0500, Bolan Meek wrote: Viktor Rosenfeld wrote: Hi list, this one is for all the regexp, shell, and editing-experts... How about us perl hackers, hunh?! Got sumpin' g'inst us, buddy!? seems to me, us perl folk are the culmination of the cross-breeding of all those geni. not that that's a good thing... :) Suppose I have a comma-seperated or tab-seperated file and I want to flip the lines and columns. So an input file like: a,1,A b,2,B c,3,C would be transformed into: a,b,c 1,2,3 A,B,C Is there an fast and easy way of doing this? Ideally through the shell or with VIM. I would RTFM, but I have no clue which manual to read. this is the kind of thing perl was designed for. [OT in a large way] i'd try something like (tmtowtdi): #!/usr/bin/perl $sep = shift || \t; # can supply separator on cmd line @ary = (); # for peace of mind for ($ln=0; ; $ln++) { chomp; $fn = 0; foreach (split(/$sep/o)) { $ary[ $fn++ ][ $ln ] = $_; #$ary[col][row] == val } } print join(\n, map {join($sep,@$_)} @ary ),\n; alarmingly, it even seems to work well on mismatched tables such as a b c 1 2 3 4 5 i ii A B C D
Re: Manipulating file content
Hello Bolan, Bolan Meek wrote: Here's the replacement code, in a script, instead of a one-liner. (I could've one-lined it, but this'll be more understandable: look! I even added _comments_) [Perl code snipped] This works perfect, well, almost perfect. It inserts a space after each comma. The fix is a no-brainer of course ($newRow[$i] .= $entry[$i],; in the first loop and a $newRow[$j] .= at the beginning of the second). Thanks heaps, Viktor -- Viktor Rosenfeld E-Mail: mailto:[EMAIL PROTECTED] HertzSCHLAG:http://www.informatik.hu-berlin.de/~rosenfel/hs/
Re: Manipulating file content
Will Trillich wrote: i'd try something like (tmtowtdi): [Perl code snipped] This one worked perfectly right out of the box. With the added value that I can supply a seperator on the command line :) and the disadvantage that I have no clue, what the code is actually doing :( [1] Big thank you, Viktor [1] It's not deleting my files, though. That's good. :) -- Viktor Rosenfeld E-Mail: mailto:[EMAIL PROTECTED] HertzSCHLAG:http://www.informatik.hu-berlin.de/~rosenfel/hs/
Re: Manipulating file content
Bolan Meek wrote: this one is for all the regexp, shell, and editing-experts... How about us perl hackers, hunh?! Got sumpin' g'inst us, buddy!? Of course not! How could I?! :) Well, you could use regexp in sed, or use an awk script, but if I had only 3x3 matrices to transform, in text, I'd perl -e 'for ($i=0;$i3;++$i){;@entry = split ',';print $i[0],$i[1],$i[2]\n;}' with a file directed into it, and stdout redirected to a file. This ..., well ..., it doesn't work. At first I thought that you meant @entry[x] in your last line, but that doesn't help either. I always get 3 pairs of commas without the values. Besides, the way I read the code, it doesn't do anything usefull, because a line with values seperated by commas, will become exactly the same line. But then again, I don't know anything about Perl, so this is just guessing. Matrixes with unpredetermined columns or rows become slightly trickier, but only by 1) keeping track of the length/breadth, and 2) nesting another loop. What about matrixes with a different number of columns and rows (e.g. 4x3 or 123x234)? MfG Viktor -- Viktor Rosenfeld E-Mail: mailto:[EMAIL PROTECTED] HertzSCHLAG:http://www.informatik.hu-berlin.de/~rosenfel/hs/
Re: Manipulating file content
Viktor Rosenfeld wrote: Bolan Meek wrote: this one is for all the regexp, shell, and editing-experts... How about us perl hackers, hunh?! Got sumpin' g'inst us, buddy!? Of course not! How could I?! :) Well, we members of the Perl Hackers Anti-Defamation League sometimes are a little touchy... Well, you could use regexp in sed, or use an awk script, but if I had only 3x3 matrices to transform, in text, I'd perl -e 'for ($i=0;$i3;++$i){;@entry = split ',';print $i[0],$i[1],$i[2]\n;}' with a file directed into it, and stdout redirected to a file. This ..., well ..., it doesn't work. (Head under the arm) Guess I ought have _tested_ that first, hunh? At first I thought that you meant @entry[x] in your last line, Actually, I meant $entry[x]... but that doesn't help either. Yes, I see that now. I always get 3 pairs of commas without the values. Besides, the way I read the code, it doesn't do anything usefull, because a line with values seperated by commas, will become exactly the same line. But then again, I don't know anything about Perl, so this is just guessing. No, you're right. That was a quick stupid of me. Matrixes with unpredetermined columns or rows become slightly trickier, but only by 1) keeping track of the length/breadth, and 2) nesting another loop. What about matrixes with a different number of columns and rows (e.g. 4x3 or 123x234)? OK. I'll figure out why I'm not getting from my split what I expect, correct my script, extend it for arbitrary matrices, and get back to you. (Boy, do I feel foolish...) That's what I get for not actually _testing_ my code. I guess I'd better resign from the PHADL, since I'm going to give us a bad name
Re: Manipulating file content
Viktor Rosenfeld wrote: Bolan Meek wrote: this one is for all the regexp, shell, and editing-experts... How about us perl hackers, hunh?! Got sumpin' g'inst us, buddy!? Maybe I ought have said perl slackers. (Well, I can get away with that since I resigned from PHADL). Well, you could use regexp in sed, or use an awk script, but if I had only 3x3 matrices to transform, in text, I'd WARNING! BAD CODE! WARNING! perl -e 'for ($i=0;$i3;++$i){;@entry = split ',';print $i[0],$i[1],$i[2]\n;}' with a file directed into it, and stdout redirected to a file. Here's the replacement code, in a script, instead of a one-liner. (I could've one-lined it, but this'll be more understandable: look! I even added _comments_) #!/usr/bin/perl while () { chop; #chop \n $origCols = (@entry = split /,/); for ($i = 0; $i $origCols; ++$i) { $newRow[$i] .= $entry[$i], ; } } for ($j = 0; $j $i; ++$j) { chop $newRow[$j]; # chop trailing ' ' chop $newRow[$j]; # chop trailing ',' print $newRow[$j]\n; } What about matrixes with a different number of columns and rows (e.g. 4x3 or 123x234)? I tested it with a 5x3 matrix. It ought to handle arbitrary sizes, but it doesn't test for irregular matrices, you know: something like 1,2,3,4,5 A,B,C,D,E,F,G a,b,c I,II,III,IV,V
Manipulating file content
Hi list, this one is for all the regexp, shell, and editing-experts on this list. Suppose I have a comma-seperated or tab-seperated file and I want to flip the lines and columns. So an input file like: a,1,A b,2,B c,3,C would be transformed into: a,b,c 1,2,3 A,B,C Is there an fast and easy way of doing this? Ideally through the shell or with VIM. I would RTFM, but I have no clue which manual to read. TIA, Viktor -- Viktor Rosenfeld E-Mail: mailto:[EMAIL PROTECTED] or: mailto:[EMAIL PROTECTED] HertzSCHLAG:http://www.informatik.hu-berlin.de/~rosenfel/hs/
Re: Manipulating file content
Viktor Rosenfeld wrote: Hi list, this one is for all the regexp, shell, and editing-experts... How about us perl hackers, hunh?! Got sumpin' g'inst us, buddy!? Suppose I have a comma-seperated or tab-seperated file and I want to flip the lines and columns. So an input file like: a,1,A b,2,B c,3,C would be transformed into: a,b,c 1,2,3 A,B,C Is there an fast and easy way of doing this? Ideally through the shell or with VIM. I would RTFM, but I have no clue which manual to read. Well, you could use regexp in sed, or use an awk script, but if I had only 3x3 matrices to transform, in text, I'd perl -e 'for ($i=0;$i3;++$i){;@entry = split ',';print $i[0],$i[1],$i[2]\n;}' with a file directed into it, and stdout redirected to a file. Matrixes with unpredetermined columns or rows become slightly trickier, but only by 1) keeping track of the length/breadth, and 2) nesting another loop. TIA, YWATF (You're Welcome After The Fact)