Re: How to remove [], {}, and other characters, rendering numeric values in a CSV file ?



Erasmo Perez wrote:
Hi dear list:

Hello,

Thank you very much for you great help in solving my past issue,
regarding the removing of the trailing commas and points.

Thank you very much indeed :-)

Now, my last (I hope) issue.

I got another text file in the following format:

cluster[1] = { 2 3 4 8 10 14 }
cluster[2] = { 25 26 29 32 }
cluster[3] = { 1 5 6 7 11 12 13 17 18 22 }
cluster[4] = { 9 19 21 23 24 27 28 30 31 33 34 }

and I need to tranform it in a new CSV file that starts each line with
the "cluster" value (the square-brackets enclosed value), followed by
its {}-bracket enclosed list values (in the same order), as is shown
below:

1,2,3,4,8,10,14
2,25,26,29,32
3,1,5,6,7,11,12,13,17,18,22
4,9,19,21,23,24,27,28,30,31,33,34

How could I accomplish it using Perl ?

$ perl -le'
my @x = (
"cluster[1] = { 2 3 4 8 10 14 }",
"cluster[2] = { 25 26 29 32 }",
"cluster[3] = { 1 5 6 7 11 12 13 17 18 22 }",
"cluster[4] = { 9 19 21 23 24 27 28 30 31 33 34 }",
);

for my $line ( @x ) {
print $line;
print join ",", $line =~ /\d+/g;
}
'
cluster[1] = { 2 3 4 8 10 14 }
1,2,3,4,8,10,14
cluster[2] = { 25 26 29 32 }
2,25,26,29,32
cluster[3] = { 1 5 6 7 11 12 13 17 18 22 }
3,1,5,6,7,11,12,13,17,18,22
cluster[4] = { 9 19 21 23 24 27 28 30 31 33 34 }
4,9,19,21,23,24,27,28,30,31,33,34



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: Array size limit in Perl
    ... The data format is as follows, ... And my perl code is as follows. ... Have you tried printing this string rather than just executing it, ... examine the sorted array. ...
    (perl.beginners)
  • Re: Using References to Formats, Examining Scalars With Devel::Peek
    ... the format to be able to refer to it by another name didn't occur to me. ... Seems most of the fields are the same as for the rest of the Perl ... from integer to double and how frequently the string portions are updated. ... Using an operator or a built-in with a double and an int usually converts ...
    (comp.lang.perl.misc)
  • Re: Implementing Storables STORABLE_freeze and STORABLE_thaw
    ... > Hello Perl People, ... > I'm interested in cloning some Perl objects that I've written, ... Whichever format conveys the necessary information to let you unfreeze ... and put whatever I needed for serialization into the list of refs that ...
    (comp.lang.perl.misc)
  • Re: Tool zur Dateiformat-Analyse
    ... einzelnen Teile rauskramen und dann dem ein oder anderen spezifischen ... CPAN Modul vorwerfen, sollte das kein größeres Problem sein. ... "man perl" liest sich schon durchaus beeindruckend. ... Das macht Sinn für große Dateien mit unbekanntem Format ...
    (de.comp.sys.mac.misc)
  • Text::Autoformat usage for a rookie
    ... I try to use Perl in my work to solve repetitive tasks. ... I now badger the group. ... I get a usage error. ... It seems as though people use it to format simple strings to ...
    (comp.lang.perl.misc)