Perl File convertor??

Discussion in 'Perl Misc' started by Davy, Dec 16, 2005.

  1. Davy

    Davy Guest

    Hi all,

    I am new to Perl (I used to be a C/C++ user). And I want to write a
    file convertor in Perl to learn it. And I use ActivePerl.

    // Input.txt context
    s 1 2
    a 4 5
    a 9 8

    // Output.txt context I want
    Sub (wire1, wire2);
    Add (wire4, wire5);
    Add (wire9, wire8);

    BTW, how to debug perl script in ActivePerl, can I set breakpoint in
    it?

    Any suggestions will be appreciated!
    Best regards,
    Davy
    Davy, Dec 16, 2005
    #1
    1. Advertising

  2. Davy

    John Bokma Guest

    "Davy" <> wrote:

    > Hi all,
    >
    > I am new to Perl (I used to be a C/C++ user). And I want to write a
    > file convertor in Perl to learn it. And I use ActivePerl.
    >
    > // Input.txt context
    > s 1 2
    > a 4 5
    > a 9 8
    >
    > // Output.txt context I want
    > Sub (wire1, wire2);
    > Add (wire4, wire5);
    > Add (wire9, wire8);


    What have you been trying so far?

    > BTW, how to debug perl script in ActivePerl, can I set breakpoint in
    > it?


    Yes, perldoc perldebug

    Or look up perldebug in the HTML documentation (it's under core
    documentation).


    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
    John Bokma, Dec 16, 2005
    #2
    1. Advertising

  3. Davy wrote:
    > Hi all,
    >
    > I am new to Perl (I used to be a C/C++ user). And I want to write a
    > file convertor in Perl to learn it. And I use ActivePerl.
    >
    > // Input.txt context
    > s 1 2
    > a 4 5
    > a 9 8
    >
    > // Output.txt context I want
    > Sub (wire1, wire2);
    > Add (wire4, wire5);
    > Add (wire9, wire8);


    I would split() the line,
    _s_ubstitute the 's' with 'Sub' and the 'a' with 'Add'.
    And then just print() the new first element, followed by ' (wire', followed
    by the second element, followed by ', wire', followed by the third element,
    followed by ');'.

    > BTW, how to debug perl script in ActivePerl, can I set breakpoint in
    > it?


    Yes. See 'perldoc perldebug' for details.

    jue
    Jürgen Exner, Dec 16, 2005
    #3
  4. Davy

    Anno Siegel Guest

    [Newsgroups trimmed]

    Jürgen Exner <> wrote in comp.lang.perl.misc:
    > Davy wrote:
    > > Hi all,
    > >
    > > I am new to Perl (I used to be a C/C++ user). And I want to write a
    > > file convertor in Perl to learn it. And I use ActivePerl.
    > >
    > > // Input.txt context
    > > s 1 2
    > > a 4 5
    > > a 9 8
    > >
    > > // Output.txt context I want
    > > Sub (wire1, wire2);
    > > Add (wire4, wire5);
    > > Add (wire9, wire8);

    >
    > I would split() the line,
    > _s_ubstitute the 's' with 'Sub' and the 'a' with 'Add'.


    That would take one s/// for each expansion. I'd expect a real program
    to have more than just "a" and "s", so that doesn't scale well. I'd
    prefer a hash lookup in this case:

    my %expand = (
    a => 'Add',
    s => 'Sub',
    );

    Then expansion is a single statement. It's also more maintainable and
    presumably faster.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 16, 2005
    #4
  5. Davy

    Dr.Ruud Guest

    Anno Siegel:
    > Jürgen Exner:
    >> Davy:


    >>> // Input.txt context
    >>> s 1 2
    >>> a 4 5
    >>> a 9 8
    >>>
    >>> // Output.txt context I want
    >>> Sub (wire1, wire2);
    >>> Add (wire4, wire5);
    >>> Add (wire9, wire8);

    >>
    >> I would split() the line,
    >> _s_ubstitute the 's' with 'Sub' and the 'a' with 'Add'.

    >
    > That would take one s/// for each expansion.


    Not necessarily:

    #!/usr/local/bin/perl

    use strict;
    use warnings;

    while (<DATA>) {

    s/^(?:
    a(?{'Add'})
    |s(?{'Sub'})
    |z(?{'Zzz'})
    )
    [[:blank:]](.)
    [[:blank:]](.)$
    /$^R (wire$1, wire$2);/x;

    print;
    }

    __DATA__
    s 1 2
    a 4 5
    a 9 8
    t 0 0
    a 1 2
    z 2 2

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Dec 16, 2005
    #5
  6. Davy

    Anno Siegel Guest

    Dr.Ruud <> wrote in comp.lang.perl.misc:
    > Anno Siegel:
    > > Jürgen Exner:
    > >> Davy:

    >
    > >>> // Input.txt context
    > >>> s 1 2
    > >>> a 4 5
    > >>> a 9 8
    > >>>
    > >>> // Output.txt context I want
    > >>> Sub (wire1, wire2);
    > >>> Add (wire4, wire5);
    > >>> Add (wire9, wire8);
    > >>
    > >> I would split() the line,
    > >> _s_ubstitute the 's' with 'Sub' and the 'a' with 'Add'.

    > >
    > > That would take one s/// for each expansion.

    >
    > Not necessarily:


    Well, the immediate implementation with s/// does.

    > #!/usr/local/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > while (<DATA>) {
    >
    > s/^(?:
    > a(?{'Add'})


    No fair -- code insertions.

    > |s(?{'Sub'})
    > |z(?{'Zzz'})
    > )
    > [[:blank:]](.)
    > [[:blank:]](.)$
    > /$^R (wire$1, wire$2);/x;


    Ah, I'd entirely forgotten about the $^R variable. That's a good use.

    > print;
    > }
    >
    > __DATA__
    > s 1 2
    > a 4 5


    [...]

    I think the purpose of code insertions is to prove that everything can be
    done with a regex :)

    I don't use them much. Last time I looked there were still scoping issues
    with surrounding lexicals (but don't take my word for it), so the designation
    as experimental was still appropriate.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 16, 2005
    #6
  7. Davy

    Dr.Ruud Guest

    Anno Siegel:
    > Dr.Ruud:
    >> Anno Siegel:
    >>> Jürgen Exner:


    >>>> I would split() the line,
    >>>> _s_ubstitute the 's' with 'Sub' and the 'a' with 'Add'.
    >>>
    >>> That would take one s/// for each expansion.

    >>
    >> Not necessarily:

    >
    > Well, the immediate implementation with s/// does.
    >
    >> [...]

    >
    > No fair -- code insertions.


    I just knew you would say that. And you're right.


    > Ah, I'd entirely forgotten about the $^R variable. That's a good use.


    I had never used it before.


    > I think the purpose of code insertions is to prove that everything
    > can be done with a regex :)
    >
    > I don't use them much. Last time I looked there were still scoping
    > issues with surrounding lexicals (but don't take my word for it), so
    > the designation as experimental was still appropriate.


    Good remark. For those who want to know more, see perlre.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Dec 16, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hyaXM=?=

    .PDF Convertor

    =?Utf-8?B?Q2hyaXM=?=, Jul 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    395
    Paul Clement
    Jul 27, 2005
  2. Spartanicus
    Replies:
    9
    Views:
    462
    David Dorward
    Oct 21, 2004
  3. =?Utf-8?B?QVNQIERldmVsb3Blcg==?=

    RTF to html convertor

    =?Utf-8?B?QVNQIERldmVsb3Blcg==?=, Sep 26, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    327
    David Wier
    Sep 26, 2006
  4. Joey

    TTS(Text to Speech) Convertor.

    Joey, Jan 26, 2006, in forum: C Programming
    Replies:
    2
    Views:
    344
    Kenneth Brody
    Jan 26, 2006
  5. ravi4udude

    C code to flowchart convertor

    ravi4udude, Mar 14, 2006, in forum: C Programming
    Replies:
    4
    Views:
    635
    santosh
    Mar 26, 2006
Loading...

Share This Page