sub attrib_type {
foreach $value (@attributes){
print "Is $value an attribute (y or n)? ";
$response = ;
chomp($response);
if ($response =~ "y" || $response =~ "Y"){
print "response is $response\n";
print "configure as an attribute\n";
}
else {
print "response is $response\n";
print "configure as a parser\n";
}
}
It looks to me as though you have a foreach {} loop that doesn't end properly (well, it is ended by what you seem to think, based on indentation, is the end of the subroutine). Maybe it is just a typo in your posted code ... ?
It seems more likely (based on the code) that you would be repeatedly prompted for attribute value answers ... and since you're not printing a \n on the prompt line, it may look as though you're not really stepping through the attributes ... but still, I would think that your responses would be printed and would result in a carriage return/line feed ...
Can you check your curly braces and make sure the code properly closes out the foreach loop, and post the actual output, please?
Update: Oops. Reading from STDIN flushes STDOUT.
if ($value_add gt 0){
foreach $value (@value_add){
print "Is $value an attribute (y or n)? ";
$response = ;
chomp($response);
if ($response =~ "y" || $response =~ "Y"){
print "response is $response\n";
print "configure as an attribute\n";
}
else {
print "response is $response\n";
print "configure as a parser\n";
}
} # end foreach $value (@value_add)
}
So, unless you declare $value up above this snippet, you aren't using strict, which is foolhardy. You seem to be using a string comparison operator (gt) for a numerical comparison, which doesn't seem the best way to skin this cat.
In any case, the following standalone snippet works fine under perl 5.8.8 on a Solaris platform:
#!/usr/local/bin/perl
use strict;
use warnings;
Main: {
my @param_names = ('elevation','location_type','point_name','position','road_type','road_type_description','vegetation');
categorize_attributes(\@param_names);
}
sub categorize_attributes {
my ($value_ref) = @_;
my @value_add = ();
@value_add = @{$value_ref} if ref($value_ref) eq 'ARRAY';
if (scalar(@value_add) > 0){
foreach my $value (@value_add){
print "Is $value an attribute (y or n)? ";
my $response = ;
chomp($response);
if ($response =~ /^y$/i){
print "response is $response\n";
print "configure as an attribute\n";
}
else {
print "response is $response\n";
print "configure as a parser\n";
}
} # end foreach $value (@value_add)
}
}
Update: Modified code sample so that the user input was handled by a subroutine, as specified by the OP.
my @attributes = qw(first second third);
attrib_type();
sub attrib_type {
foreach $value (@attributes){
print "Is $value an attribute (y or n)? ";
$response = ;
chomp($response);
if ($response =~ "y" || $response =~ "Y"){
print "response is $response\n";
print "configure as an attribute\n";
}
else {
print "response is $response\n";
print "configure as a parser\n";
}
}
}
What I would do (and often have done myself, either for a Perlmonks post, or just on my own), is to strip out parts of the code, a little bit at a time, and continuously retry it. Your goal should be to make it as close to the original post as you can. (And do, please, add "use strict;" and "use warnings;" at the top of the code first).
If at any point it starts working, assuming the changes you made were small each time, you will probably say "Aha!", and have reached enlightenment on your own.
If not, you can post the new, shortened, and hopefully less inelegant code here, and one of use will say "Aha!", and point you towards enlightenment.
I have to refer to it during the rest of the script as main::@whatever
No, you're correct that you shouldn't have to do that. Can you post or /msg me a very short example? I'll be happy to show you how to fix it.
Here is the Perl documentation for use strict;.
use strict; use warnings; @existingattributes = ();Produces the error:
use strict; use warnings; # initialize arrays @main::existingattributes = ();
Very simply, because you need my in your declaration:
use strict; use warnings; my @existingattributes = ();
Then you will no longer get the error.
That's what use strict does; it enforces your declaration of variables with my or our. Using [doc://my] lets you use it like a global variable (except that it's really a lexical variable), and it's visible only within the lexical scope in which it's declared. If that's at the top of the program file, then the entire program can use it.
However, it you need to share variables between files, you may need to use [doc://our] instead.
perlmonks.org content © perlmonks.org and bw, ikegami, liverpole, ptum, rodion
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03