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

Please see notes in code it is only part of the code but it will show the issue. What am I doing wrong?
if ($edate eq "$date") #the array works fine when doing this... { @Names = (1,2,3); #but does not work the below way even though $times is pulled from a f +ile and it is pulling the same string as above. { @Names = ($times); print "<select>\n"; foreach (@Names) { print "<option>$_</option>$_\n"; } print "</select>\n"; } }

Thanks

<code> tags added by davido.

2006-04-20 Retitled by davido, as per Monastery guidelines
Original title: 'Need help'

Replies are listed 'Best First'.
Re: Loading data into an array from a file problem
by GrandFather (Saint) on Apr 20, 2006 at 23:01 UTC

    What is the content of $times? Can you provide a script that can actually be run to reproduce the problem that you are having? "does not work" does not tell us what you expected, nor what you got.

    The following code may be a good starting point:

    use strict; use warnings; while (<DATA>) { chomp; my @Names = split ','; print "@Names\n"; } __DATA__ Peter,Fred,Joe Sam,Anna,Sue

    Prints:

    Peter Fred Joe Sam Anna Sue

    DWIM is Perl's answer to Gödel
Re: Loading data into an array from a file problem
by eff_i_g (Curate) on Apr 20, 2006 at 22:45 UTC
    #!/usr/bin/perl -w use strict; my $string = '1,2,3'; my @names; ### This prints one line (what you're getting) @names = ($string); print +(join '--' => @names), "\n"; ### This prints multiple lines (what you want) @names = split ',' => $string; print +(join '--' => @names), "\n";
    P.S. Next time use a descriptive title. We know you need help if you're here.
Re: Loading data into an array from a file problem
by davido (Cardinal) on Apr 20, 2006 at 22:41 UTC

    I'm not sure what behavior you're expecting, but $times represents a single scalar value, so @Names contains only one element, and your foreach loop will only execute one iteration. Is that what you're seeing? If not, explain what you are expecting, and what you are seeing instead.


    Dave

Re: Loading data into an array from a file problem
by eric256 (Parson) on Apr 20, 2006 at 22:33 UTC

    What am I doing wrong?

    You arn't asking a question about your actual code and you didn't use <code></code> tags.


    ___________
    Eric Hodges
      oops why does the first work and the second don't
      @Names = (1,2,3); @Names = ($same_as_above_pulled_from_file);

        Because (1,2,3) is three elements, and ($same_as_above_pulled_from_file) is just a single scalar value, not a list of several values.

        Maybe you mean:

        @names = split /,\s*/, $same_as_above_pulled_from_file;

        ...chomp and fiddle with split to get the desired results.


        Dave

        In order to recieve help you will need to put a little more work than that in ;) Show us both the working and the non working versions (the whole version so we can run it and see what it does) and explain what "works" and "doesn't work" mean to you. Your expectations and ours may differ greatly so the more you say explicitly the less there is for us to guess about.


        ___________
        Eric Hodges