On Tue, 31 Jul 2007, amiribarksdale wrote:

> Hey folks. I'm new here, and this is my first post.
>
> I'm trying to figure out a way to manage multiple file uploads. I have a
> form that allows users to upload arbitrary numbers of files, which I then
> need to process and save on the server. I have a javascript that allows this
> arbitrary number of files: by clicking a link, you create a new form field
> that adds a numeral to the end of the field name. The %ARGS hash looks like
> this for a single file:
>
> file => 273141
> title => Back to
> id => 7
> number => 1
> creation_date => 2007
>
> A multiple upload %ARGS looks like this:
>
> file => 273141
> title => Back to
> id => 7
> number => 1
> creation_date => 2007
>
> file-2 => that'shot8
> title-2 => Natural
> id-2 => 6
> number-2 => 2
> creation_date-2 => 1985
>
> file-3 => Introduction
> title-3 => Stuff Like This
> id-3 => 12
> number-3 => 3
> creation_date-3 => 1998
>
> I created a loop that allows me to handle the %ARGS data:
>
>        my $i = 2;
>
>        while ( $i <= $ARGS{'numberfiles'} ) {
>
>            my %thash;
>
>
>            foreach my $key ( sort( keys %ARGS ) ) {
>                if ( $key =~ /creation_date-$i/ ) {
>                    $thash{'creation_date'} = $ARGS{$key};
>                }
>                if ( $key =~ /title-$i/ ) {
>                       $thash{'title'} = $ARGS{$key};
>                }
>                if ( $key =~ /number-$i/ ) {
>                    $thash{'number'} = $ARGS{$key};
>                }
>                if ( $key =~ /rate-$i/ ) {
>                    $thash{'rate'} = $ARGS{$key};
>                }
>                if ( $key =~ /type-$i/ ) {
>                       $thash{'type'} = $ARGS{$key};
>                }
>                if ( $key =~ /id-$i/ ) {
>                    $thash{'id'} = $ARGS{$key};
>                }
>                if ( $key =~ /file-$i/ ) {
>                       $thash{'file'} = $ARGS{$key};
>                }
>            }
>
>            my $nfile = $api->newBean('file');
>       }
>   $nfile->save($api);
>   $i++;
> }

        I've never had to deal with file uploading, but the following code 
should be a shorter version of what you have above:

----------------------------------------
foreach $key (keys %ARGS) {
        $basekey = $key;
        $num = ($basekey !~ s/-(\d+)$//) ? 1 : $1;
        $bighash->{$num}->{$basekey} = $ARGS{$key};
}

foreach $filenum (keys %$bighash) {
        $file = $bighash->{$filenum};
        # Do something with $file here
        $nfile = $api->newBean('file');
        $nfile->save($api);
}
----------------------------------------

        Ok, so I left out the "my" statements, but still, it's shorter and 
faster, and will automatically deal with new kinds of keys.  Tip: Data 
Structures + Algorithms = Programs.  If you do clever things with data 
structures, you can usually have a simpler program.  Designing your data 
structure before you start your algorithm can help.

        Anyway, hopefully adding an extra loop will no longer seem too 
onerous.  Or you could try to figure out some way to link the two loops 
together with a data structure.

        :)


---------------------------------------------------------------------
| Name: Tim Nelson                 | Because the Creator is,        |
| E-mail: [EMAIL PROTECTED] | I am                           |
---------------------------------------------------------------------

----BEGIN GEEK CODE BLOCK----
Version 3.12
GCS d+++ s+: a- C++$ U+++$ P+++$ L+++ E- W+ N+ w--- V- 
PE(+) Y+>++ PGP->+++ R(+) !tv b++ DI++++ D G+ e++>++++ h! y-
-----END GEEK CODE BLOCK-----

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users

Reply via email to