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

Hey guys I am new to perl and I am developing a CGI script which takes input from the user and assign it to the hash reference $ input...I am generating all the dates in between the range given by the user and storing it in the array...For each date stired in @date I wants to assign the value of array to the hash reference ...following is the code which I am using.....kindly let me know how to access every element of the array using foreach loop . Another Question is can I assign the user input through param in foreach loop...

foreach (@date){ # create hash reference variable for user inputs as well as every date + in array @date $user_input->{pp_branch} = param('pp_branch'); $user_input->{host_name} = param('host_name'); $user_input->{controller} = param('controller'); $user_input->{start_date} = ;#I wann assign here each element of array + unless the array gets empty; # .... some code.......... }

The doubts are silly ....but I am confused.....so wants to confirmed @ it ...After all PerlMonks is the place where we can get faithful information:-)

Thanks in advance !!!!

Replies are listed 'Best First'.
Re: Questionon foreach loop syntax
by kennethk (Abbot) on Mar 10, 2010 at 20:28 UTC
    If you use the syntax

    foreach (@date){

    to access an array, Perl will automatically store the current value from the array in the magic variable $_, hence your assignment statement might look like:

    $user_input->{start_date} = $_;

    Alternatively, if you use the syntax

    foreach $your_variable (@date){

    the current array value is stored in $your_variable (or whatever scalar you put there). For more information, see Foreach Loops in perlsyn.

Re: Questionon foreach loop syntax
by ssandv (Hermit) on Mar 10, 2010 at 21:37 UTC

    Perlmonks is a place where other Perl users, on a strictly voluntary basis, help one another solve problems. Instead of posting code and asking us if it's right, post code *and the results of running it*, and ask us to resolve the differences between what you thought it would do and what it did. There's almost no reason to post code without running it first. Monks may be wrong about what it will do when executed, but the compiler won't. You should ask the compiler what your code does (by running it).

    As for your code, it's not at all clear what you're trying to accomplish, but as it stands, you're doing scalar assignment to the same variables on each pass through the loop, as near as I can tell, so everything's going to get clobbered. That doesn't seem like what you want, but I could be wrong.

Re: Question on foreach loop syntax
by Svante (Sexton) on Mar 10, 2010 at 20:32 UTC

    It really would help if you could build sentences. Sentences are terminated by a single dot ("."). There should be no whitespace before punctuation, but one space after.

    Anyway, the thing you are looking for is $_.

    foreach (@array) { print $_; }

    prints each element. You can also assign the elements to a variable like this:

    foreach my $line (@array) { print $line; }

    I have no idea what you could mean with your second question.