Connecting Tech Pros Worldwide Help | Site Map

Very Basic String Operators!!

Newbie
 
Join Date: Sep 2007
Posts: 24
#1: Sep 8 '07
Hey Guys, would anyone care to explain whats going on;

I have a few codes and i don't know why one works and the other doesn't. I'm playing around with concatenation operators "," and "."

These are my codes. ALL THE CODES WORKED EXCEPT FOR THE LAST ONE!!!


CODE ONE
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. #stringoperator.plx
  3. use warnings;
  4. print "print", " several", " strings", " here", "\n";
  5. print "print" . " several" . " strings" . " here" . "\n"; 
  6.  
For the second code i tried using both concatenations in the same line of code, just to see if I'd get any errors but they both gave me desired results.

CODE TWO
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. #string1.plx
  3. use warnings;
  4. print "Four sevens are ". 4 * 7 ,"\n";
  5. print " Four sevens are ", 4 * 7 ."\n";
  6.  

CODE THREE
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. #string2.plx
  3. use warnings;
  4. print "G0 JOHNNY" ." GO!!"x2, "\n";
  5.  
CODE FOUR
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. #string2.plx
  3. use warnings;
  4. print "G0 JOHNNY" ," GO!!"x2, "\n";
  5.  
CODE FIVE
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. #string2.plx
  3. use warnings;
  4. print "G0 JOHNNY" ." GO!!"x2, "\n";
  5.  
CODE SIX
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. #string2.plx
  3. use warnings;
  4. print "G0 JOHNNY" ." GO!!"x2. "\n";
  5.  
Error Reads As;
String found where operator expected at <filename.plx> line 4, near "2. "\n""
(Missing operator before "\n"?)
syntax error at <filename.plx> line 4, near 2. "\n""
Execution of <filename.plx> aborted due to compilation errors.

I
Newbie
 
Join Date: Sep 2007
Posts: 24
#2: Sep 8 '07

re: Very Basic String Operators!!


I should mention that i know the error has to do with the operator "." between x2 and "\n" but i do not know why.

Thanks for your replies!!!
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Sep 8 '07

re: Very Basic String Operators!!


Quote:

Originally Posted by autodidact

I should mention that i know the error has to do with the operator "." between x2 and "\n" but i do not know why.

Thanks for your replies!!!

The problem is you have a dot right after the number 2, perl seems to be interpreting that as a decimal number. The real problem is the poor formatting of your code, you need a space between the 2 and the dot so that the intention is clear:

Expand|Select|Wrap|Line Numbers
  1. print "G0 JOHNNY" ." GO!!" x2 . "\n";
perl is confused by the poor formatting and made a guess that you are using a decimal number instead of concatenation. That is a good example of how not to format code.

Or parenthesis around the middle part would let perl know what you are wanting to do:

Expand|Select|Wrap|Line Numbers
  1. print "G0 JOHNNY".(" GO!!"x2)."\n";
but this is better written using commas:

Expand|Select|Wrap|Line Numbers
  1. print "G0 JOHNNY"," GO!!"x2, "\n";
because the print function is a list operator and expects a comma seperated list of strings or scalars to print. Read the perl style guide for guidance on how to write well formatted perl code:

perldoc: Perl style guide

This is one facet of perl that some programmers find iritating. Perl allows for some very sloppy and ambiguous formatting, and that can lead to problems. Other languages have stricter formatting rules.
Newbie
 
Join Date: Sep 2007
Posts: 24
#4: Sep 8 '07

re: Very Basic String Operators!!


Thanks Kevin.

That helped!!!!
Reply