I have the reading in part figured out, :) but how do I substitute the variables for their values (which will change as I run the program)?

I confess that I'm uncertain of the exact question you are asking, so I'll try to answer the easiest question I can make of your post. :-)

Are you asking:

How can I print out the values of scalar variables?

If so, you're in luck. Perl makes this much easier than other C derived languages. The one way to print out values it like this:

print "I like " . $x . "\n";

This code uses the dot concatentation operator to join strings together. There is a little magic going on here, in that the value of $x is treated like a string. Consider this code:

$x = '0001'; print "I like " . $x; print "but I like " . ($x + 0)

The first print statement will yield "I like 0001", but the second prints "but I like 1". The expression with the + operator in the second print statement yeilds a number, rather than a string. Therefore, the leading zeros are removed before the concatentation occurs ($x remains '0001').

Perl provides an even simpler way to print out scalars -- interpolation. Perl will attempt to substitute the value for scalars and arrays in double quoted strings.

$x = '0001'; print "I like $x";

This code prints "I like 0001". Remember that single quoted strings do not interpolate variables.

$x = '0001'; print 'I like $x';

This prints "I like $x". You can prevent variable interpolation in double quoted strings by escaping the sigil (the punctuation mark that precedes variable names).

$x = 'timmah!'; print "\$x is $x";

This prints "$x is timmah!".

For more tips on string interpolation, checkout the perlop manpage under the section "Gory details of parsing quoted constructs".


In reply to Re: variable expansion by jjohn
in thread variable expansion by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.