CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How do I say "if a hash link exists in a source code?"

 



alexloas
Novice

Apr 11, 2010, 12:54 PM

Post #1 of 4 (722 views)
How do I say "if a hash link exists in a source code?" Can't Post

Ok, so pretend this is this the source code.txt:

Original program:
" example
bool a
bool b
bool c

a := true
b := false
c := a + b

puts c

now I want to create a hash to translate this code into perl:
my $a = 0;
my $b = 0;
my $c = 0;
$a = 1;
$b = 0;
$c = $a || $b;
print "c is " . ($c ? "true" : "false") . "\n";

Here is my hash to translate the source code to perl code :
$translate{'bool'} = 'my'; #basically this just says turn "bool" to "my"

How do write an if statement in perl saying that "if the word "bool" exists in source code.txt then print "my" otherwise print nothing"?


(This post was edited by alexloas on Apr 11, 2010, 1:08 PM)


7stud
Enthusiast

Apr 11, 2010, 2:37 PM

Post #2 of 4 (716 views)
Re: [alexloas] How do I say "if a hash link exists in a source code?" [In reply to] Can't Post

You don't need an if statement; you can just use the substitution operator s//:


Code
use strict; 
use warnings;
use 5.010;


open my $INFILE, '<', 'data1.txt'
or die "Couldn't open input file: $!";

open my $OUTFILE, '>', 'data2.txt'
or die "Couldn't open output file: $!";

while (my $line = <$INFILE>) {
$line =~ s/bool/my/g;
print {$OUTFILE} $line;
}


close $INFILE;
close $OUTFILE;


data1.txt:

Code
example  
bool a
bool b
bool c, bool d

if bool e

a := true
b := false
c := a + b


data2.txt:

Code
example  
my a
my b
my c, my d

if my e

a := true
b := false
c := a + b


Look at man perlop, section "Regexp Quote Like Operators" for s//.


(This post was edited by 7stud on Apr 11, 2010, 2:50 PM)


alexloas
Novice

Apr 11, 2010, 3:39 PM

Post #3 of 4 (706 views)
Re: [7stud] How do I say "if a hash link exists in a source code?" [In reply to] Can't Post

I should have mentioned this earlier but I would like to know how could you implement that code you gave me if it were in a subroutine in a file named translate.pl for example and instead of two data files you gave me, what if there was just one (just for efficiency). The output I am interested in getting is this:

if Data 1.txt is :

Code
example   
bool a
bool b
bool c, bool d

if bool e

a := true
b := false
c := a + b



And I type something like "perl translate.pl data1.txt" in the terminal to get an output like this:


Code
Original Code 
example
bool a
bool b
bool c, bool d

if bool e

a := true
b := false
c := a + b

Translated Code:
example
my a
my b
my c, my d

if my e

a := true
b := false
c := a + b


What would I have to implement in my subroutine to substitute "bool" to "my"?


7stud
Enthusiast

Apr 11, 2010, 7:34 PM

Post #4 of 4 (700 views)
Re: [alexloas] How do I say "if a hash link exists in a source code?" [In reply to] Can't Post

A program and a subroutine are the same thing. If you wrap a program with:


Code
sub some_name { 

#program here

return $something

}


...then it is a subroutine.

There are different measures of efficiency. But if you want to use one file, then open the file for reading, read all the lines into an array:


Code
my @lines = <$INFILE>;


Then close the file and reopen it for appending:


Code
open my $OUTFILE, '>>', 'data1.txt' ...


Then use a for loop to step through each line in the array:


Code
for my $line (@lines) { 

}


do the substitution inside the for loop, and then print the line to $OUTFILE.

Is it efficient if your computer has 2GB of RAM, and the file size is 4GB? I would argue that having your program try to read a 4GB file on disk into an array of lines in memory and watching your program crash is not efficient.

Oh, yeah. Command line arguments go into perl's global array: @ARGV. To get the file name, it's typically done like this:


Code
my $filename = shift @ARGV;


You can then use $filename in open() in place of the string 'data1.txt'.


(This post was edited by 7stud on Apr 11, 2010, 7:42 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives