Professional Web Applications Themes

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. #1

    Default 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. #2

    Default 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. #3

    Default 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. #4

    Default 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. #5

    Default 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. #6

    Default 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

Similar Threads

  1. How to transform PDF 6.0 to PDF 4.0
    By salathe@freesurf.ch in forum Adobe Acrobat SDK
    Replies: 0
    Last Post: March 31st, 01:36 PM
  2. Use Transform box
    By chung1107@adobeforums.com in forum Adobe Illustrator Windows
    Replies: 2
    Last Post: June 2nd, 03:51 PM
  3. transform ?
    By johnnson12 in forum Macromedia Director 3D
    Replies: 0
    Last Post: April 13th, 02:34 PM
  4. Illustrator CS transform bug
    By Marcus_Taylor@adobeforums.com in forum Adobe Illustrator Macintosh
    Replies: 0
    Last Post: April 5th, 04:14 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139