View Full Version : Add strings together

08-11-2002, 11:11 PM
What I am trying to do is simple enough, I know some JavaScript so?

var name = "corrie";
var holder = "hi my name is "+name+" what's yours?";

but how to write the above in perl is baffling me, I'm just a beginner in perl and the '$ % @' things are really messing with my head, so that's probably my problem, I know the first is a variable, second is an associative array, and third is a plane old array, but the associations between them (how you can reference the @person with $person[1]) is just confusing me, if anyone knows any good tutorials that cover this in great detail please let me know.


08-13-2002, 12:34 AM
Its quite slow round here these days, no matter I've solved this, if anyone's interested?

$name = "such and such "."is linked into this" ## yes, a dot. That was what was holding me back, a dot.

Well, it is usually the smallest things that get me.

08-13-2002, 03:11 AM
Once you get over the initial shock of the funny symbols you'll find them really useful, for example, if you were doing a Perl version of your JavaScript code, you'd go:

$name = "Corrie";
$holder = "Hi, my name is $name, what's yours?";

As you can see, you can just write the name of the variable within a double-quoted string and Perl will interpolate it for you.

As for the $array[3] bit, it might seem a little weird at first, but if you think about it carefully it actually makes more sense to use the $ symbol here. Since what you're actually accessing with this syntax is a single scalar value, it makes sense to use the single scalar symbol...

To be honest just learning the basic scalar, array and associative array stuff is worth it, it makes sense once you get the hang of it :)

If you want to learn Perl, the best learning book, especially if you're not an experienced programmer already is Learning Perl by Randal Schwartz and Tom Christiansen (I think). But the definitive Perl book, which is slightly meatier and pretty comprehensive is called Programming Perl by Larry Wall, Jon Orwant and Tom Christiansen, also called the Camel Book.

There are millions of beginner's tutorials for Perl out there though, just do a search on Google and some will turn up.

08-13-2002, 12:53 PM
Thanks Mouldy_Goat, I've seen that in a tutorial somewhere before (and should have remembered), its just such a big step from JavaScript, putting a variable in a string with JavaScript that way would have been unthinkable, so naturally I never thought of it :)

I'll have a look for those books, thanks again