0

I am trying to run a loop on the strings in the $ACES_1_key but I get Can't use string ... as an ARRAY ref while "strict refs" in use.

my $ACES_1_key = ("`NIL-RETURN`,`ASSESSEE-NAME`,`LTU`,`MONTH`,`RETURN-YEAR`,`REGISTRATION-NUMBER`");

foreach my $key (@$ACES_1_key) {
  print $key;
}
4
  • 2
    Well, your $ACES_1_key simply isn't an arrayref, therefore it can't be used as one, much like I cannot use a bicycle as an airplane. Do you want to parse that string into an array? This won't happen automatically. Either do it with regexes, or use the Text::CSV parser. Commented Sep 17, 2013 at 9:48
  • 1
    Are the backticks part of the array elements, or just an attempt to use as a quote character? Commented Sep 17, 2013 at 9:48
  • You seem to be confused and probably need to read a bit about arrays and hashes. Even about variables, I guess (referring to my $ACES_1_key). Commented Sep 17, 2013 at 9:49
  • @SlavenRezic,@devnull i am inserting the data into MYSQL from XML and the values in $ACES_1_key are the column headers as they have hyphen so i have included backticks....i wanted to check if column exists hence was trying to run the loop Commented Sep 17, 2013 at 9:57

2 Answers 2

2

You should avoid using capital letters in lexical variable names. They are reserved for global identifiers such as package names.

If you are trying to set up an array reference in the first place then you want something like this:

my $aces_1_key = [ qw[ NIL-RETURN ASSESSEE-NAME LTU MONTH RETURN-YEAR REGISTRATION-NUMBER ] ];

foreach my $key (@$aces_1_key) {
  print $key, "\n";
}

output

NIL-RETURN
ASSESSEE-NAME
LTU
MONTH
RETURN-YEAR
REGISTRATION-NUMBER

Alternatively, if you have a string that you need to split into individual substrings then there are a few ways to. The program below shows one. It splits the string at the commas to produce a list of quoted substrings. Then the quotes are removed inside the loop using tr//. The output is identical to that of the previous example.

my $aces_1_key=("`NIL-RETURN`,`ASSESSEE-NAME`,`LTU`,`MONTH`,`RETURN-YEAR`,`REGISTRATION-NUMBER`");

foreach my $key (split /,/, $aces_1_key) {
  $key =~ tr/`//d;
  print $key, "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

my @ACES_1_key = (
    'NIL-RETURN',
    'ASSESSEE-NAME',
    'LTU',
    'MONTH',
    'RETURN-YEAR',
    'REGISTRATION-NUMBER'
);
foreach my $key (@ACES_1_key) {
    print $key;
}

(I requoted the elements and made the variable an actual array)

(and didn't test it)

2 Comments

can i have backticks in array as i am using these as MYSQL column names as these are having hyphen and it's needed to add backtick to run a query.
You can have backticks as long as you put them inside the regular single quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.