Welcome to Discuss Everything Forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.


 

Tags for this Thread

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    MARY S
    MARY S's Avatar
    Guest

    Unix/Linux Perl Programming?

    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. #2
    joe.attaboy
    joe.attaboy's Avatar
    Guest
    I just ran it on a Solaris box, exactly as you posted it here, and it worked.

    I notice your she-bang line at the top says "#!/bin/bash/perl". Take out the "bash " part (should be "#!/bin/perl"). You only add bash in a she-bang line where you're writing a shell script and you want the script to be run by that shell. Bash has nothing to do with perl.

    @array is an array data type in perl, like a list. Your array has three elements: a zero, a sequence of numbers from 2 through 6, and the string "abc".

    "pop" removes the last element of the array and places it in the variable ($a). So, $a should be "abc".

    "shift" removes the first element of the array and places it in the variable ($b). So, $b should be 0.

    When you refer to an array (@array) using a scalar ($array), you get returned the value of the array. So "print($array)" will print the values of the array out. In this case, you will see:

    What is 2 3 4 5 6?

    This is because you popped "abc" off the end and shifted the 0 off the front. So the number range is the only thing left in the array.

    If you're getting a syntax error, you made a mistake typing it out. Look carefully.

    You're lucky. I usually leave a smart-aleck remark about not helping people with their homework. But you're not going to find a lot of perl programmers here.

  3. #3
    Peter K
    Peter K's Avatar
    Guest
    It runs on my system. I'm wondering why your error is on line 5 when according to your listing it should be on line 2? Have you got some junk in there or blank lines?

    I attach the output from my system.

 

 

Quick Reply Quick Reply

Click here to log in


What is the sum of 36 and 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 09-13-2010, 12:38 PM
  2. metacharacters and the ll command UNIX/LINUX?
    By davaware in forum Discuss Law
    Replies: 0
    Last Post: 09-12-2010, 07:48 AM
  3. Replies: 1
    Last Post: 08-12-2009, 02:46 AM
  4. Replies: 0
    Last Post: 03-17-2009, 11:18 PM
  5. Replies: 1
    Last Post: 12-10-2008, 03:00 PM

Bookmarks

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •