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

I want to be able to do: for (@array){ $output = <some...command>; #### then be able to associate $array with $output values and then be able to store $array and its $values into an array $a->$array=$output; <..something like this..> and then be able to print each element of $array and its associated $output }

Replies are listed 'Best First'.
Re: array and hash
by jdporter (Paladin) on Jun 09, 2005 at 20:37 UTC
    Probably an array of arrays is what you want. A-like so:
    my @array = get_items(); # whatever. the input. my @results; # each element is a pair: [ input, output ] for my $item ( @array ) { my $output = some_function($item); push @results, [ $item, $output ]; } # now print: for ( @results ) { my( $input, $output ) = @$_; print "Input: $input. Output: $output\n"; }
    Something along those lines.
      Of course, there are tons of different ways to do something like this.

      You could store the results in a hash rather than array, if inputs are all unique strings:

      my %results; for ( @array ) { $results{$_} = some_func($_); }
      You could make the results array contain hash-based records instead of array-based:
      my @results; for ( @array ) { push @results, { input => $_, output => some_func($_) }; }
      Etc. There's also shortcuts for the loop:
      my %results = map { ( $_ => some_func($_) ) } @array;
      my @results = map { { input => $_, output => some_func($_) } } @array;
      Etc.
      Hello all, Thank you for all the fast suggestions u gave. I will implement every one of your suggestions.
Re: array and hash
by kirbyk (Friar) on Jun 09, 2005 at 20:38 UTC
    Boy, did you pick the right language. Try:
    my %hash; for my $element (@array) { $hash{$element} = <some command>; } ... for $element (@array) { # Keep the same order as the original array print "For $element, output was $hash{$element}\n"; }
    This would be much harder in something that wasn't perl.

    -- Kirby, WhitePages.com

Re: array and hash
by jbrugger (Parson) on Jun 09, 2005 at 20:40 UTC
    Well, you can do something like this
    #untested, no perl on the current machine... my @array= qw ( command1 command2 command3); my %hash = (); foreach (@array) { $output = eval($_); $hash->{$_}= $output; } sub command1() { # do something return 1; }
    can be done shorter, more efficient, but gives the idea to you.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: array and hash
by djohnston (Monk) on Jun 09, 2005 at 20:48 UTC
    You can accomplish something like what (I think) you're asking using a hash, so long as your array will not contain duplicates.
    my %hash; $hash{$_} = some_output($_) for @array; # Print each in arbitrary order like so: print $hash{$_} for keys %hash; # Print in original sequence by reusing the array: print $hash{$_} for @array; sub some_output { local $_; # Insert mystery code here }
    Update: Fixed mundane detail.
Re: array and hash
by cbrandtbuffalo (Deacon) on Jun 09, 2005 at 20:36 UTC
    If you attempted something and it didn't turn out quite right, it is much easier for helpers to read if you use code tags around the actual code. As it is, I'm not sure what you are asking. Maybe you can provide some data and and example of what you have tried so far?
Re: array and hash
by mrborisguy (Hermit) on Jun 09, 2005 at 20:38 UTC

    I think that's a reasonable desire. I personally don't want to do that because I don't presently have a need for it, but I think it is good that you want to be able to do that.

    (In other words, what is your question?)

        -Bryan