Very Basic String Operators!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • autodidact
    New Member
    • Sep 2007
    • 24

    Very Basic String Operators!!

    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
    [code=perl]
    #! /usr/bin/perl
    #stringoperator .plx
    use warnings;
    print "print", " several", " strings", " here", "\n";
    print "print" . " several" . " strings" . " here" . "\n";
    [/code]
    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
    [code=perl]
    #! /usr/bin/perl
    #string1.plx
    use warnings;
    print "Four sevens are ". 4 * 7 ,"\n";
    print " Four sevens are ", 4 * 7 ."\n";
    [/code]


    CODE THREE
    [code=perl]
    #! /usr/bin/perl
    #string2.plx
    use warnings;
    print "G0 JOHNNY" ." GO!!"x2, "\n";
    [/code]
    CODE FOUR
    [code=perl]
    #! /usr/bin/perl
    #string2.plx
    use warnings;
    print "G0 JOHNNY" ," GO!!"x2, "\n";
    [/code]
    CODE FIVE
    [code=perl]
    #! /usr/bin/perl
    #string2.plx
    use warnings;
    print "G0 JOHNNY" ." GO!!"x2, "\n";
    [/code]

    CODE SIX
    [code=perl]
    #! /usr/bin/perl
    #string2.plx
    use warnings;
    print "G0 JOHNNY" ." GO!!"x2. "\n";
    [/code]
    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
    Last edited by numberwhun; Sep 8 '07, 01:24 PM. Reason: add code tags
  • autodidact
    New Member
    • Sep 2007
    • 24

    #2
    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!!!

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      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:

      Code:
      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:

      Code:
      print "G0 JOHNNY".(" GO!!"x2)."\n";
      but this is better written using commas:

      Code:
      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.

      Comment

      • autodidact
        New Member
        • Sep 2007
        • 24

        #4
        Thanks Kevin.

        That helped!!!!

        Comment

        Working...