# transform 3*0.0 into 0.0, 0.0, 0.0 - PERL Miscellaneous

CM <starobs99> writes: > Hi, > I have a little probleme. As output of a code, I have something like: > 9935.69043, 549.563843, 500., 6 > 10048.5518, 549.505005, 500., 7 > 10305.9766, 549.381104, 500., 8 > 3*0.E+0, 0 > 3*0.E+0, 0 > 3*0.E+0, 0 > > The problem is comming from the 3*0.E+0, which I want to transfrom into: > 0.E+0, 0.E+0, 0.E+0 > > The solution should not be dependet of the both values, I mean I need > a general way to transform > > N*blabla > into > blabla, blabla, blabla, .... N times. s/(\d+)\*(^[,]+)/join ", ", ...

1. ## Re: transform 3*0.0 into 0.0, 0.0, 0.0

CM <starobs99> writes:
> Hi,
> I have a little probleme. As output of a code, I have something like:
> 9935.69043, 549.563843, 500., 6
> 10048.5518, 549.505005, 500., 7
> 10305.9766, 549.381104, 500., 8
> 3*0.E+0, 0
> 3*0.E+0, 0
> 3*0.E+0, 0
>
> The problem is comming from the 3*0.E+0, which I want to transfrom into:
> 0.E+0, 0.E+0, 0.E+0
>
> The solution should not be dependet of the both values, I mean I need
> a general way to transform
>
> N*blabla
> into
> blabla, blabla, blabla, .... N times.
s/(\d+)\*(^[,]+)/join ", ", ( \$2 ) x \$1/eg;
> PS: I don't know anything to perl, except it is certainly the
> apropriate language for solving my problem... So if possible, provide
> a fully useable script so that I just need to type:
> removestars.ps input.dat output.dat
Oh sorry, handn't realised you were pan-handling. Oh well I've answered
the question now so I'll hit send anyhow.

--
\\ ( )
. _\\__[oo
.__/ \\ /\
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest

2. ## Re: transform 3*0.0 into 0.0, 0.0, 0.0

Brian McCauley wrote:
> CM <starobs99> writes:
....
> s/(\d+)\*(^[,]+)/join ", ", ( \$2 ) x \$1/eg;
[^----------^^

--
Bob Walton

Bob Walton Guest

3. ## Re: transform 3*0.0 into 0.0, 0.0, 0.0

Bob Walton wrote:
> Brian McCauley wrote:
>
>> CM <starobs99> writes:
>
> ...
>
>> s/(\d+)\*(^[,]+)/join ", ", ( \$2 ) x \$1/eg;
>
>
> [^----------^^
>
Thanks to all, I finaly adopted the following program (just for people
who could have a similar question) :

--------------------------------------------------------------------
#!/usr/bin/perl

# Transform e.g. 3*4.5 into 4.5 4.5 4.5
# Transform , into _space_
# Usage: removestars.pl input.dat > output.dat
#
open(INPUT, "<\$ARGV[0]") || die "Cannot open \$ARGV[0]";
while (<INPUT>)
{
\$a =\$_;
\$a =~ s/(\d+)\*(\S+)(,)/print "\$2 " for 1 .. \$1/e;
\$a =~ s/(\d+)\*(\S+)(\$)/print "\$2 " for 1 .. \$1/e;
\$a =~ s/,/ /g;
print \$a;
}
close(INPUT);
--------------------------------------------------------------------

CM Guest

4. ## Re: transform 3*0.0 into 0.0, 0.0, 0.0

CM <starobs99> writes:
> Thanks to all, I finaly adopted the following program (just for people
> who could have a similar question) :
>
> --------------------------------------------------------------------
> #!/usr/bin/perl
Always:
use strict;
use warnings;

Do not wait for your failure to do so to cause you pain and
embarassement, do it now.
> # Transform e.g. 3*4.5 into 4.5 4.5 4.5
> # Transform , into _space_
> # Usage: removestars.pl input.dat > output.dat
> #
> open(INPUT, "<\$ARGV[0]") || die "Cannot open \$ARGV[0]";
Don't waste your time. Just use the magic ARGV filehandle.
> while (<INPUT>)
> {
> \$a =\$_;
Either use a named variable or use the default variable \$_.

If you do use a named variable make it lexically scoped.

Don't use \$a
> \$a =~ s/(\d+)\*(\S+)(,)/print "\$2 " for 1 .. \$1/e;
> \$a =~ s/(\d+)\*(\S+)(\$)/print "\$2 " for 1 .. \$1/e;
Eeek!

You really should not be printing in the RHS of the s///

You should not use a for loop in a non-void context.

Do you really want to rely on their being whitespace after the comma?

Do you really want only to consider a maximum of 2 * in a line?
> \$a =~ s/,/ /g;
> print \$a;
> }
> close(INPUT);
> --------------------------------------------------------------------
So did you really want...

'1, 4*2,3, 4*5, 3*6'

....to be reansformed into...

'2,3 2,3 2,3 2,3 6 6 6 1 '

....?

I would have thought you'd have wanted:

'1 2 2 2 2 3 5 5 5 5 6 6 6'

--
\\ ( )
. _\\__[oo
.__/ \\ /\
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest

5. ## Re: transform 3*0.0 into 0.0, 0.0, 0.0

CM <starobs99> wrote in message news:<beeump\$4dgt0\$1ID-189674.news.dfncis.de>...
> Brian McCauley wrote:
> > CM <starobs99> writes:
> >
> >
> >>Thanks to all, I finaly adopted the following program (just for people
> >>who could have a similar question) :
> >>
> >>--------------------------------------------------------------------
> >>#!/usr/bin/perl
> >
> >
> > Always:
> > use strict;
> > use warnings;
> >
> > Do not wait for your failure to do so to cause you pain and
> > embarassement, do it now.
>
> OK. So I put these two commands on the top of the file?
Yes.

>># Transform e.g. 3*4.5 into 4.5 4.5 4.5
> >># Transform , into _space_
> >># Usage: removestars.pl input.dat > output.dat
> >>#
> >>open(INPUT, "<\$ARGV[0]") || die "Cannot open \$ARGV[0]";
> >
> >
> > Don't waste your time. Just use the magic ARGV filehandle.
> >
>
> How?
while (<ARGV>) {
# Do things
}

You can even abreviate it to

while (<>) {
# Do things
}

The ARGV file handle is magicially each of the files listed on the
command line in turn.
> >>while (<INPUT>)
> >> {
> >> \$a =\$_;
> >
> >
> > Either use a named variable or use the default variable \$_.
> >
> > If you do use a named variable make it lexically scoped.
> >
> > Don't use \$a
>
> What do you mean?
The one can think of special \$_ variable is 'the current record'.

If you are doing several simple operations on a record letting it be
in \$_ rather than a named variable is faster to write and faster to
run,

while (<>) {
s/whatever/whatever else};
}
> Avoiding the use of \$a will turn the program to look
> like what?

> >
> >> \$a =~ s/(\d+)\*(\S+)(,)/print "\$2 " for 1 .. \$1/e;
> >> \$a =~ s/(\d+)\*(\S+)(\$)/print "\$2 " for 1 .. \$1/e;
> >
> >
> > Eeek!
> >
> > You really should not be printing in the RHS of the s///
>
> If I don't print, it doens't work...
And if you do, it doesn't either.
> I certainly missed something.
You are using s/// as a loop control structure with an RHS that has a
side effect of causing output to STDOUT. This output goes to the
program's output stream there and then, ir does not get put back into
the appropriate string you are processing.
> > You should not use a for loop in a non-void context.
>
> Well, and without loops?
I have no idea what you mean by that. The RHS of s///e is evaluated
and the return value is used for something. AFAIK it is undefined
what happens if you use a for loop in such a context.
> > Do you really want to rely on their being whitespace after the comma?
>
> No,
So why did you make you regex insist that there was?
> > Do you really want only to consider a maximum of 2 * in a line?
>
> No, it can be more...
So why did you only have two s/// niether having a /g ? Each can at
most handle one instance.
>
> >
> >> \$a =~ s/,/ /g;
> >> print \$a;
> >> }
> >>close(INPUT);
> >>--------------------------------------------------------------------
> >
> >
> > So did you really want...
> >
> > '1, 4*2,3, 4*5, 3*6'
> >
> > ...to be reansformed into...
> >
> > '2,3 2,3 2,3 2,3 6 6 6 1 '
> >
> > ...?
> >
> > I would have thought you'd have wanted:
> >
> > '1 2 2 2 2 3 5 5 5 5 6 6 6'
>
> Yep exactly! But I laso want to remove any coma, even if no * around...
> In conclusion, could you send me a more perly code?
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
# chomp() technically redundant here - but a good habit...
chomp;

# replace comma with space
tr/,/ /;

# Process the "*"s
s/(\S+)\s*\*\s*(\S+)/"\$1 " x \$2/eg;

# Normalise the whitespace
s/^\s+//; # Strip leading whitespace
s/\s+\$//; # Strip trailing whitespace
s/\s+/ /g; # All whitespace becomes a single space

print "\$_\n";
}
__END__
nobull@mail.com Guest

6. ## Re: transform 3*0.0 into 0.0, 0.0, 0.0

CM <starobs99> spits TOFU in my face...
> Thanks a lot,
Please express your gratitude by refraining from top-posting and
full-quoting henseforth.
nobull@mail.com Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•