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



Hi Yitzle:

Thank you very much for your suggestion:

Here is my perl file: clusters.pl

#! /usr/bin/perl
use warnings;
use strict;
while (my $line = <>) {
$line = ~/cluster\[(\d)+\] = {([\d ]+)}/ or die;
my @vals = split(/+/,"$1 $2");
print join(",",@vals). "\n";
}

my input file (clusters.in) is:

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

When I input to the shell

bash-3.00$ ./clusters.pl clusters.in > clusters.out

I do get the following error message:

Quantifier follows nothing in regex; marked by <-- HERE in m/+ <--
HERE / at ./clusters.pl line 6.

and got nothing written at the output file

Where am I making the mistake ?

Am I wrongly invocating the perl code ?

Thank you for any help

Regards

Erasmo


On 26/06/2008, yitzle <yitzle@xxxxxxxxxxxxxxxxxxxxx> wrote:
On Thu, Jun 26, 2008 at 3:28 PM, Erasmo Perez <perez.erasmo@xxxxxxxxx>
wrote:
Hi dear list:

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 ?

How could I remove the square-brackets, the {}'s , the "=" sign and
the word "cluster" from the input file, rendering the same number
sequence, but now just separated by commas ?

Thank you very much for your great help, it is _saving_ my neck in the job
:-)

Regards

Erasmo

#!/usr/bin/perl
use warnings;
use strict;

while ( my $line = <> ) {
$line =~ /cluster\[(\d)+\] = {([\d ]+)}/ or die;
my @vals = split( / +/, "$1 $2" );
print join(",", @vals) . "\n";
}

.



Relevant Pages

  • Re: debugger exiting
    ... strict and warnings pragmas. ... I think portraying Perl as a command-line tool limits it to fewer platforms than ... work only as a Unix shell command line. ...
    (perl.beginners)
  • Re: dns querry script.
    ... use warnings; ... use strict; ... C:\Dload> perl dns.pl ...
    (comp.lang.perl.misc)
  • Re: Any way to access global variable in Perl script from one module file?
    ... use strict; ... use warnings; ... a separate process has a completely separate memory space. ... There is probably no reason to create a new perl ...
    (comp.lang.perl.misc)
  • Re: Debug Help Please
    ... In Perl there is the expression TIMTOWTDI which means that you will probably get different opinions on "The Right Way" to do something in Perl. ... use strict; ... That is a specious argument because you can disable specific strictures or warnings in local scope: ... for all Failures ...
    (perl.beginners)
  • Re: List Variable becomes undefined inexplicably
    ... warnings and strictures very often find bugs in programs. ... It is inconsiderate of your audience to post without warnings and strict. ... There is a Perl and there is a perl. ... chomp $LINE; ...
    (comp.lang.perl.misc)