Unix/Linux Perl Programming?

Discussion in 'Linux / Unix Discussion' started by gerrydevis, Jan 12, 2010.

  1. gerrydevis

    gerrydevis Banned

    3
    0
    0
    The following is a section of a program. Show the value of each variable at each step:

    @array =(0, 2..6, abc); What is @array?
    $a = pop(@array); What is $a?
    $b = shift(@array); What is $b?
    What is @array?

    Can someone please help me. This is what I have so far but it seems like its telling me that I have an error and it won't execute.

    #!/bin/bash/perl
    @array = ( 0, 2..6, abc );
    $a = pop(@array);
    $b = shift(@array);
    print (What is @array?\n);
    print ($array\n);
    print ($a\n);
    print ($b\n);
    print (What is @array?\n);
    print ($array\n);

    the error that i am recieving is
    ./lab10.pl: line 5: syntax error near unexpected token `('
    ./lab10.pl: line 5: `@array = (0, 2..6, abc);'

    I'd be greatful if you could help me out! Thank you.
    No the way I have it there is what I have written on my program.
     
  2. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    I believe the point is to understand what the compiler/interpreter is doing for you by working out the state at each point in your head...

    At no point does it ask you to write a program, point no.1, read the question !
     
  3. Rover977

    Rover977 Byte Poster

    242
    11
    40
    This question is testing your understanding of arrays and list assignments, and use of the pop and shift array operators in Perl. The abc should really be quoted ie 'abc' or "abc" but Perl accepts unquoted strings (even though this issues a warning if warnings are switched on via -w on the command line, or if the 'use warnings;' pragma is added at the head of your source file).

    To see the output of the program you've written, place double quotes around the contents within each of the print statements, eg :-

    print ("$array\n");

    instead of :-

    print ($array\n);

    Also, Perl allows you to dispense with the brackets, so you could just write :-

    print "$array\n";

    Don't use single quotes as this does not interpolate the \n end-of-line escape character.

    Note you could also write :

    print @array;

    to output an array but this adds no spaces between the elements eg you get "23456".

    print "@array"; will add the spaces, eg you get "2 3 4 5 6".
     
    Last edited: Jan 21, 2010
    Certifications: A+, Network+, Cisco CCNA

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.