Printing "" and $iIN

V

vedpsingh

Hi,
I want to print the below mentioned text.

------------------------------
multved_HQrow0IN := "0000000000000000";
multved_HQrow1IN := "0000000000000000";
multved_HQrow2IN := "0000000000000000";
multved_HQrow3IN := "0000000000000000";
multved_HQrow4IN := "0000000000000000";
----------------------------

For this I have written the code given below:
--------------------------------------------------------

use strict; # important pragma
use warnings; # another important pragma

print "Enter value of instantiations ? "; # print out the question

my $i; # \declare" the variable
my $j;
$j = <STDIN>; # ask for the username
chomp($j); # remove \new line"

for ($i = 0; $i<= $j; $i++) {

open (MYFILE, '>>MultRecover.txt');

print MYFILE "

multved_HQrow$iIN := "0000000000000000";

\n ";
}

close (MYFILE);

--------------------------------------------------

I get error in 2 things:
(1) $iIN -----> i.e. I after $i

(2) " " are special characters. What do I do to print them.

Thanks
Ved
 
D

DJ Stunks

Hi,
I want to print the below mentioned text.

------------------------------
multved_HQrow0IN := "0000000000000000";
multved_HQrow1IN := "0000000000000000";
multved_HQrow2IN := "0000000000000000";
multved_HQrow3IN := "0000000000000000";
multved_HQrow4IN := "0000000000000000";

:) very good! thank you!
print "Enter value of instantiations ? "; # print out the question

my $i; # \declare" the variable

never declare ahead of time like in other languages. declare in the
smallest possible scope. in your case:

for (my $i = 0; $i < $j; $i++) {
my $j;
$j = <STDIN>; # ask for the username
chomp($j); # remove \new line"

you could do these together:

chomp( my $j = said:
for ($i = 0; $i<= $j; $i++) {

see above.
open (MYFILE, '>>MultRecover.txt');

always, yes ALWAYS, check OS operations (open, system, fork, etc).
also, you should begin to get in the habit of using lexical
filehandles, and the three-argument version of open().

my $file = 'MultRecover.txt';
open my $MYFILE, '>>', $file or die "Could not open '$file': $!\n";
print MYFILE "

multved_HQrow$iIN := "0000000000000000";

\n ";
}

close (MYFILE);

--------------------------------------------------

I get error in 2 things:
(1) $iIN -----> i.e. I after $i
${i}IN

(2) " " are special characters. What do I do to print them.

delimit it - print "this is a doublequote: \". ";

I would probably use printf though:

printf 'multved_HQrow%dIN := "0000000000000000";%s', $i, "\n";

HTH,
-jp
 
J

Jürgen Exner

for ($i = 0; $i<= $j; $i++) {

for $i (0..$j)

is much easier to read and to understand.
print MYFILE "

multved_HQrow$iIN := "0000000000000000";

\n "; [...]
I get error in 2 things:
(1) $iIN -----> i.e. I after $i

Because Perl tries to print the variable $iIn which obviously doesn't exist
in your program.
Enclose the variable name in curly brackets:
multved_HQrow${i}IN
(2) " " are special characters. What do I do to print them.

Escape them with a leading backslash:
:= \"0000000000000000\";

jue
 
K

Klaus

Michele said:
Or, since it has not been pointed out yet, use alternate delimiters:

my $string=qq|multved_HQrow${i}IN := "0000000000000000"|;

I like using alternate delimiters, such as qq|...|

I just would like to add that you can even use curly braces qq{...}
which nest correctly with the ${i} inside the string:

my $string=qq{multved_HQrow${i}IN := "0000000000000000"};
 
J

J. Gleixner

print "Enter value of instantiations ? "; # print out the question

Others have pointed out the solution, I thought I'd mention that $i and
$j are not very helpful variables.
my $i; # \declare" the variable
my $j;
$j = <STDIN>; # ask for the username

If $j is what you'd refer to as a username, then use $username, or
$max_instantiations.. Something which makes the value of the variable,
more clear.
for ($i = 0; $i<= $j; $i++) {
If $i is really just a simple counter, then it's fine, however maybe
something like $row or $instantiation, would be more readable.
 
I

Ian Wilson

For this I have written the code given below:

When I'm learning a new language, I tend to deliberately over-comment as
reminder to myself of elementary concepts. This seems to be what you
are doing. When posting to a newsgroup I'd consider whether that level
of commenting is appropriate to my audience.

I'd also aim for something a bit more informative.
use strict; # Restrict unsafe constructs
print "Enter value of instantiations ? "; # print out the question

my $i; # \declare" the variable

I'd get into the habit of explaining the *purpose* of the variable not
simply restating the statement in English. I find that choosing
descriptive variable names can be more helpful than adding comments.

<snip>
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top