Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Problems printing contents of array

by chuleto1 (Beadle)
on Jul 07, 2005 at 14:20 UTC ( #473108=perlquestion: print w/replies, xml ) Need Help??

chuleto1 has asked for the wisdom of the Perl Monks concerning the following question:

Please advise... I am having problems running the line below:
print "@s340Strings"; Error message: In string, @s340Strings now must be written as \@s340Strings at file.p +l line 55, near "@s340Strings" Execution of aborted due to compilation errors.
Please help, you are my only hope.

Replies are listed 'Best First'.
Re: Problems printing contents of array
by jhourcle (Prior) on Jul 07, 2005 at 14:45 UTC

    The error could also mean that there isn't an array '@s340Strings' in your code.

    You may have typo'd the name of the array, or it may be a subset of that... if you wanted to print the array '@s' and then immediately the string literal '340Strings' (which would probably be bad, as it'll likely look like part of the last item in the array), you could do:

    print "@{s}340Strings";

    If you're just trying to print the literal string '@s340Strings', then either switch to single quotes, so it won't try to do variable expansion on you, or escape the @ with a \, as the error message suggested.

Re: Problems printing contents of array
by jdporter (Chancellor) on Jul 07, 2005 at 14:22 UTC
    Among other things, it means you're running an old version of perl, and it's time to upgrade.
Re: Problems printing contents of array
by kutsu (Priest) on Jul 07, 2005 at 14:25 UTC

    Where do you define the array and assign to it because print "@s340Strings"; should work. Though if you want the contents of an array (an @array used in $scalar location will print the length of the array) then you want a for/foreach statement - like print for @s340Strings;

    Update: Interesting I've never run into that before, that actually would help quite a bit. Thanks!

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      Putting an array in double quotes does not impose scalar context on the array; it does an implicit call to join using $" (default is a space) as the separator. Without the double quotes, print would print the contents using $, (default is empty string) as a separator.

      Caution: Contents may have been coded under pressure.
      an @array used in $scalar location will print the length of the array

      True, but this isn't the case.

      perl -le '@A=qw(a b c d); print "@A"' a b c d
Re: Problems printing contents of array
by shiza (Hermit) on Jul 07, 2005 at 16:36 UTC
    Another way to print the contents would be by using a module called Data::Dumper

    !#/path/to/perl use Data::Dumper; print Dumper(@s340Strings); exit;
Re: Problems printing contents of array
by sapnac (Beadle) on Jul 07, 2005 at 15:26 UTC
    my @s340Strings = qw("abc", "cdef","yxx","zzz"); print "@s340Strings\n"; prints: "abc", "cdef","yxx","zzz"
    Check your code. Please check if the array is a array of scalars or array of scalars and references. try hard-coding for test purpose the array value and see if you get past this line. That would mean the data is the culprit.

    Hope it helps

      That code is correct, but very confusing. You create a two element array, $s340Strings[0] == "\"abc\"," and $s340Strings[1] == "\"cdef\",\"yxx\",\"zzz\"". That may be what you intended, but when I read it, it looks like you intend:

      my @s340Strings = qw(abc cdef yxx zzz); # or my @s340Strings = ("abc","cdef","yxx","zzz");

      Just trying to clarify...


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://473108]
Approved by kutsu
Front-paged by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2023-03-20 16:52 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.