Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple perl program problem

Status
Not open for further replies.

mkosloff

Programmer
Feb 20, 2003
12
0
0
US
i cant for the life of me figure out what's wrong with this perl program. it takes the first input, and then freezes after that, missing all the other lines. anyone who can solve this (probably simple) problem for me, i'd really appreciate it!

print "Please enter names, enter X to quit: ";
@a[0] = <STDIN>;
chomp(@a[0]);
$n = 0;

print &quot; @a &quot;;

while(@a[0] ne &quot;X&quot;)
{
while(@a[$n-1] ne 'X')
{
print &quot;Please enter name: &quot;;
@a[$n] = <STDIN>;
chomp(@a[$n]);
$n++;
}
}

@a = sort(@a);

print &quot; @a &quot;;

thanks!
matt




 
I'm not completely sure I understand the intent of the program..... however...

When using an array (@a), you indicate a single element of that array like it is a scalar with an index to the position in the array.

Given the array:
Code:
@a_list_of_colors = ('red','blue','green');
You refer to the the second item (number 1 in the array, counting from zero) like:
Code:
$color = $a_list_of_colors[1]; # $color is blue now.
In your code, when refering to one element of an array you are beginning the name of your variable with '@'. I think you want to use a '$'. Like:
Code:
$a[0] = <STDIN>; # stick STDIN in the first slot
chomp $a[0]; # chomp the value in the first slot
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
thank you! that was definitely my problem.

and fyi, the program was just a small program that alphabetizes a list of names that the user enters, a &quot;X&quot; entered will print out the list. now if only i could figure out how to keep the X out from being printed at the end..
 
Replace the line:
print &quot; @a &quot;;

with

foreach $value (@a) {
if($value ne &quot;X&quot;) {print &quot;$value\n&quot;;}
}

Roy
 
thanks! i just started trying to learn perl, this has been a huge help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top