Unix/Linux Perl Programming?

  • Thread starter Thread starter MARY S
  • Start date Start date
M

MARY S

Guest
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.
 
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.
 
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.
 
Back
Top