Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl File convertor??

Reply
Thread Tools

Perl File convertor??

 
 
Davy
Guest
Posts: n/a
 
      12-16-2005
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, wire;

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

Any suggestions will be appreciated!
Best regards,
Davy

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      12-16-2005
"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, wire;


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

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-16-2005
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, wire;


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


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-16-2005
[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, wire;

>
> 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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-16-2005
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, wire;

>>
>> 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."

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-16-2005
Dr.Ruud <rvtol+> 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, wire;
> >>
> >> 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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-16-2005
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."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 04-04-2011 10:00 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments
 



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