Ask a Question related to PERL Miscellaneous, Design and Development.
-
Brian McCauley #1
Re: transform 3*0.0 into 0.0, 0.0, 0.0
CM <starobs99@yahoo.com> writes:
s/(\d+)\*(^[,]+)/join ", ", ( $2 ) x $1/eg;> 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.
Oh sorry, handn't realised you were pan-handling. Oh well I've answered> 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
the question now so I'll hit send anyhow.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest
-
How to transform PDF 6.0 to PDF 4.0
Hi We have PDF's which were created with Adobe Professional 6.0 and saved in Acrobat 6.0 format. Now we want to save them in Acrobat 4.0 format.... -
Use Transform box
In transform box, the X & Y description, how can make it to 0 & 0, that can type the actual unit to move the object, i use this fuction in corelDraw,... -
transform ?
Hi, I was hoping someone could tell me what exactly is displayed in the transform list I want to totally understand this as Ive been tweaking... -
Transform Tool
Hi all, OK we have 3 macs here and all have AI CS, and my coworker can not use her transform tool! But no problems on the other 2 macs. She get... -
Transform Bitmap
I want transform a bitmap to 1 Depth on Lingo!!! is it posible? tks!!! -
Bob Walton #2
Re: transform 3*0.0 into 0.0, 0.0, 0.0
Brian McCauley wrote:
....> CM <starobs99@yahoo.com> writes:[^----------^^> s/(\d+)\*(^[,]+)/join ", ", ( $2 ) x $1/eg;
--
Bob Walton
Bob Walton Guest
-
CM #3
Re: transform 3*0.0 into 0.0, 0.0, 0.0
Bob Walton wrote:
Thanks to all, I finaly adopted the following program (just for people> Brian McCauley wrote:
>>>> CM <starobs99@yahoo.com> writes:
> ...
>>>> s/(\d+)\*(^[,]+)/join ", ", ( $2 ) x $1/eg;
>
> [^----------^^
>
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
-
Brian McCauley #4
Re: transform 3*0.0 into 0.0, 0.0, 0.0
CM <starobs99@yahoo.com> writes:
Always:> Thanks to all, I finaly adopted the following program (just for people
> who could have a similar question) :
>
> --------------------------------------------------------------------
> #!/usr/bin/perl
use strict;
use warnings;
Do not wait for your failure to do so to cause you pain and
embarassement, do it now.
Don't waste your time. Just use the magic ARGV filehandle.> # 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]";
Either use a named variable or use the default variable $_.> while (<INPUT>)
> {
> $a =$_;
If you do use a named variable make it lexically scoped.
Don't use $a
Eeek!> $a =~ s/(\d+)\*(\S+)(,)/print "$2 " for 1 .. $1/e;
> $a =~ s/(\d+)\*(\S+)($)/print "$2 " for 1 .. $1/e;
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?
So did you really want...> $a =~ s/,/ /g;
> print $a;
> }
> close(INPUT);
> --------------------------------------------------------------------
'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
-
nobull@mail.com #5
Re: transform 3*0.0 into 0.0, 0.0, 0.0
CM <starobs99@yahoo.com> wrote in message news:<beeump$4dgt0$1@ID-189674.news.dfncis.de>...
Yes.> Brian McCauley wrote:>> > CM <starobs99@yahoo.com> 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?
while (<ARGV>) {>>># 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?
# 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.
The one can think of special $_ variable is 'the current record'.>> >> >>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?
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?
And if you do, it doesn't either.>> >> >> >> $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...
You are using s/// as a loop control structure with an RHS that has a> I certainly missed something.
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.
I have no idea what you mean by that. The RHS of s///e is evaluated>> > You should not use a for loop in a non-void context.
> Well, and without loops?
and the return value is used for something. AFAIK it is undefined
what happens if you use a for loop in such a context.
So why did you make you regex insist that there was?>> > Do you really want to rely on their being whitespace after the comma?
> No,
So why did you only have two s/// niether having a /g ? Each can at>> > Do you really want only to consider a maximum of 2 * in a line?
> No, it can be more...
most handle one instance.#!/usr/bin/perl>>> >> >> >> $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?
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
-
nobull@mail.com #6
Re: transform 3*0.0 into 0.0, 0.0, 0.0
CM <starobs99@yahoo.com> spits TOFU in my face...
Please express your gratitude by refraining from top-posting and> Thanks a lot,
full-quoting henseforth.
nobull@mail.com Guest



Reply With Quote

