Rob Dixon wrote:
Richard Lee wrote:
Chas. Owens wrote:
On Sat, Mar 22, 2008 at 1:02 AM, Richard Lee <[EMAIL PROTECTED]> wrote:
let's say I have

 @someting = qw/1 2 3 4/;

 @something2 = qw/110 11 12/;

 @something3 = qw/20 30 40 50/;

and I get the name of array from regular expression match something like
 this from some other file

    $_ =~ /^(\S+) \s\s$/;

 so, now $1 is either something or something2 or something3,

 and if I wanted to do

 something like

 @bigarray{ @something } = split

 how can use $1 to put it inside?

 I was thinking something like

 @bigarray{ @($1) } = split ??
 but doesn't work..
 can someone advice?
snip

I am not sure what you are trying to achieve with
"@[EMAIL PROTECTED] = split", it is a hash slice, but the values in
@something don't look like keys, so I assume you didn't mean for it to
be one.  Here is how you can get an array dynamically:

#!/usr/bin/perl

use strict;
use warnings;

my %arrays = (
    something  => [qw<1 2 3 4>],
    something2 => [qw<110 11 12>],
    something3 => [qw<20 30 40 50>]
);

while (<>) {
    if (my ($key) = /^(\S+) \s\s$/) {
        if (exists $arrays{$key}) {
            print join(",", @{$arrays{$key}}), "\n";
        } else {
            print "no array named $key\n";
        }
    } else {
        print "input was in the wrong format\n";
    }
}


say I have file

--file--
something3 one two three and so on
something two two two so one
something one two three
so on and so forth



program
@something = qw/val1 val2 val3 and so forth/;
@something2 = qw/vala valb valb and so forth/;
@something3 = qw/valZ valZ1 valZ2 so forth/;


while ( <file>) {
/^(\S+) .+$/; #find out what the first word of first line is, in this case-------> something3 ----> line is something3 one two three..... @[EMAIL PROTECTED] = split # use the $1 value from above and use that name to find the appropriate array name that u should be using for key # so it will assign valZ => something3 valZ1 => one valZ2 => two


Is that clear? please let me know.

It is bad practice to use symbolic references (where one variables
contains the name of another). As Chas wrote, hard references in
combination with hashes are the alternative. Does the program below do
something like what you need?

HTH,

Rob


use strict;
use warnings;

my %index = (
  something  => [ qw/val1 val2 val3/ ],
  something2 => [ qw/vala valb valb/ ],
  something3 => [ qw/valZ valZ1 valZ2/ ],
);

my %bighash;

while (<DATA>) {
  next unless my ($key) = /^(\S+) .+$/;
  next unless my $names = $index{$key};
  @[EMAIL PROTECTED] = split;
}

use Data::Dumper;
print Dumper \%bighash;

__DATA__
something3 one two three
something two two two
something one two three


**OUTPUT**

$VAR1 = {
          'valZ2' => 'two',
          'val2' => 'one',
          'val1' => 'something',
          'valZ1' => 'one',
          'val3' => 'two',
          'valZ' => 'something3'
        };

I will try this once I get home.

thanks~

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to