Perl File convertor??

D

Davy

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
 
J

John Bokma

Davy said:
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).
 
J

Jürgen Exner

Davy said:
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
 
A

Anno Siegel

[Newsgroups trimmed]

Jürgen Exner said:
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
 
D

Dr.Ruud

Anno Siegel:
Jürgen Exner:

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
 
A

Anno Siegel

Dr.Ruud said:
Anno Siegel:

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
 
D

Dr.Ruud

Anno Siegel:
Dr.Ruud:
Anno Siegel:

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top