Re: how to replace @ inside of { }

2008-03-28 Thread Richard Lee

Just one more question,

what is the difference between,

${$score{ fred } }

and

${$score}{fred}

are they the samething?



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




Re: how to replace @ inside of { }

2008-03-28 Thread Gunnar Hjalmarsson

Richard Lee wrote:

Just one more question,

what is the difference between,

${$score{ fred } }

and

${$score}{fred}

are they the samething?


No, they are not.

The first assumes a hash named %score, e.g.

my %score = ( fred = \ 5 );

where the value of the key 'fred' is a scalar reference.

The second assumes a hash reference named $score, e.g.

my $score = { fred = 5 };

HTH

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: how to replace @ inside of { }

2008-03-28 Thread Richard Lee

Gunnar Hjalmarsson wrote:

Richard Lee wrote:

Just one more question,

what is the difference between,

${$score{ fred } }

and

${$score}{fred}

are they the samething?


No, they are not.

The first assumes a hash named %score, e.g.

my %score = ( fred = \ 5 );

where the value of the key 'fred' is a scalar reference.

The second assumes a hash reference named $score, e.g.

my $score = { fred = 5 };

HTH


awesome!!

thank you.

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




Re: how to replace @ inside of { }

2008-03-28 Thread Richard Lee

Richard Lee wrote:

Gunnar Hjalmarsson wrote:

Rob Dixon wrote:

Richard Lee wrote:

Gunnar Hjalmarsson wrote:


C:\hometype test.pl
use Data::Dumper;
my %HoA = (
something  = [ qw/val1 val2 val3 and so forth/ ],
something2 = [ qw/vala valb valc and so forth/ ],
something3 = [ qw/valZ valZ1 valZ2 so forth/ ],
);
my %HoH;
while ( DATA ) {
/^(\S+)/;
@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;
}
print Dumper \%HoH;

__DATA__
something3 one two three etc
something2 two three and so on
something one two three four five

C:\homeperl test.pl
$VAR1 = {
  'something3' = {
'so' = 'three',
'valZ2' = 'two',
'forth' = 'etc',
'valZ1' = 'one',
'valZ' = 'something3'
  },
  'something2' = {
'so' = 'so',
'valb' = 'two',
'forth' = 'on',
'valc' = 'three',
'vala' = 'something2',
'and' = 'and'
  },
  'something' = {
   'so' = 'four',
   'forth' = 'five',
   'val2' = 'one',
   'val1' = 'something',
   'val3' = 'two',
   'and' = 'three'
 }
};

C:\home


Hey, this works perfectly, thanks.

I just need to understand more on this

@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;

This is really spinning my heads in all direction trying to see if 
I can truly understand them instead of just using it.


Yes, it's not the most transparent line of code is it. The while 
loop is

equivalent to:

while ( DATA ) {
  my @values = split;
  my $key = $values[0];
  my $labels = $HoA{$key};
  my %map;
  @[EMAIL PROTECTED] = @values;
  $HoH{$key} = \%map;
}

And IMO it's better written like that.


While I agree that a few temporary variables may increase the 
readability, I believe that the OP's difficulties to grasp the code 
has little to do with the coding style. Easier to read code does not 
replace the need in this case to understand hash slices and references.


I agree. Let me get more fundametal of reference and get back to you 
guys.


thanks!!

I finally understood,

sub do_it {
my $XXXid = shift;
print \$XXXid is $XXXid\n;
for (@_) {
   if (  exists  $_-{ ABC_XXX_m }{ XXX_id }
$_-{ ABC_XXX_m }{ XXX_id } eq $XXXid ) {
   my $sdf = $_;
   for (sort keys  %{$sdf-{ ABC_XXX_m }} ) {
 print $_ = $sdf-{ ABC_XXX_m }{ $_ }\n;
   }
   }
}
}




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




Re: how to replace @ inside of { }

2008-03-28 Thread Gunnar Hjalmarsson

Richard Lee wrote:

I finally understood,

sub do_it {
my $XXXid = shift;
print \$XXXid is $XXXid\n;
for (@_) {
   if (  exists  $_-{ ABC_XXX_m }{ XXX_id }
$_-{ ABC_XXX_m }{ XXX_id } eq $XXXid ) {
   my $sdf = $_;
   for (sort keys  %{$sdf-{ ABC_XXX_m }} ) {
 print $_ = $sdf-{ ABC_XXX_m }{ $_ }\n;
   }
   }
}
}


Okay, that sub makes sense if you have a hash of hashes and:

my $id = $HoH{ABC_XXX_m}{XXX_id};
do_it( $id, \%HoH );

I guess that's progress. Congratulations! :)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: how to replace @ inside of { }

2008-03-28 Thread Richard Lee

Gunnar Hjalmarsson wrote:

Richard Lee wrote:

I finally understood,

sub do_it {
my $XXXid = shift;
print \$XXXid is $XXXid\n;
for (@_) {
   if (  exists  $_-{ ABC_XXX_m }{ XXX_id }
$_-{ ABC_XXX_m }{ XXX_id } eq $XXXid ) {
   my $sdf = $_;
   for (sort keys  %{$sdf-{ ABC_XXX_m }} ) {
 print $_ = $sdf-{ ABC_XXX_m }{ $_ }\n;
   }
   }
}
}


Okay, that sub makes sense if you have a hash of hashes and:

my $id = $HoH{ABC_XXX_m}{XXX_id};
do_it( $id, \%HoH );

I guess that's progress. Congratulations! :)


thanks for all your help guys!!!

Really took more time to understand rather than just trying things till 
it works


I really need to look into it more to make reference second nature as I 
look to use them a lot.


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




Re: how to replace @ inside of { }

2008-03-27 Thread Richard Lee

Gunnar Hjalmarsson wrote:

Rob Dixon wrote:

Richard Lee wrote:

Gunnar Hjalmarsson wrote:


C:\hometype test.pl
use Data::Dumper;
my %HoA = (
something  = [ qw/val1 val2 val3 and so forth/ ],
something2 = [ qw/vala valb valc and so forth/ ],
something3 = [ qw/valZ valZ1 valZ2 so forth/ ],
);
my %HoH;
while ( DATA ) {
/^(\S+)/;
@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;
}
print Dumper \%HoH;

__DATA__
something3 one two three etc
something2 two three and so on
something one two three four five

C:\homeperl test.pl
$VAR1 = {
  'something3' = {
'so' = 'three',
'valZ2' = 'two',
'forth' = 'etc',
'valZ1' = 'one',
'valZ' = 'something3'
  },
  'something2' = {
'so' = 'so',
'valb' = 'two',
'forth' = 'on',
'valc' = 'three',
'vala' = 'something2',
'and' = 'and'
  },
  'something' = {
   'so' = 'four',
   'forth' = 'five',
   'val2' = 'one',
   'val1' = 'something',
   'val3' = 'two',
   'and' = 'three'
 }
};

C:\home


Hey, this works perfectly, thanks.

I just need to understand more on this

@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;

This is really spinning my heads in all direction trying to see if I 
can truly understand them instead of just using it.


Yes, it's not the most transparent line of code is it. The while loop is
equivalent to:

while ( DATA ) {
  my @values = split;
  my $key = $values[0];
  my $labels = $HoA{$key};
  my %map;
  @[EMAIL PROTECTED] = @values;
  $HoH{$key} = \%map;
}

And IMO it's better written like that.


While I agree that a few temporary variables may increase the 
readability, I believe that the OP's difficulties to grasp the code 
has little to do with the coding style. Easier to read code does not 
replace the need in this case to understand hash slices and references.



I agree. Let me get more fundametal of reference and get back to you guys.

thanks!!

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




Re: how to replace @ inside of { }

2008-03-25 Thread Gunnar Hjalmarsson

Rob Dixon wrote:

Richard Lee wrote:

Gunnar Hjalmarsson wrote:


C:\hometype test.pl
use Data::Dumper;
my %HoA = (
something  = [ qw/val1 val2 val3 and so forth/ ],
something2 = [ qw/vala valb valc and so forth/ ],
something3 = [ qw/valZ valZ1 valZ2 so forth/ ],
);
my %HoH;
while ( DATA ) {
/^(\S+)/;
@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;
}
print Dumper \%HoH;

__DATA__
something3 one two three etc
something2 two three and so on
something one two three four five

C:\homeperl test.pl
$VAR1 = {
  'something3' = {
'so' = 'three',
'valZ2' = 'two',
'forth' = 'etc',
'valZ1' = 'one',
'valZ' = 'something3'
  },
  'something2' = {
'so' = 'so',
'valb' = 'two',
'forth' = 'on',
'valc' = 'three',
'vala' = 'something2',
'and' = 'and'
  },
  'something' = {
   'so' = 'four',
   'forth' = 'five',
   'val2' = 'one',
   'val1' = 'something',
   'val3' = 'two',
   'and' = 'three'
 }
};

C:\home


Hey, this works perfectly, thanks.

I just need to understand more on this

@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;

This is really spinning my heads in all direction trying to see if I 
can truly understand them instead of just using it.


Yes, it's not the most transparent line of code is it. The while loop is
equivalent to:

while ( DATA ) {
  my @values = split;
  my $key = $values[0];
  my $labels = $HoA{$key};
  my %map;
  @[EMAIL PROTECTED] = @values;
  $HoH{$key} = \%map;
}

And IMO it's better written like that.


While I agree that a few temporary variables may increase the 
readability, I believe that the OP's difficulties to grasp the code has 
little to do with the coding style. Easier to read code does not replace 
the need in this case to understand hash slices and references.


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: how to replace @ inside of { }

2008-03-24 Thread Richard Lee

Gunnar Hjalmarsson wrote:

Chas. Owens wrote:
On Sat, Mar 22, 2008 at 11:53 PM, Richard Lee [EMAIL PROTECTED] 
wrote:

snip

@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;

This is really spinning my heads in all direction trying to see if I 
can

truly understand them instead of just using it.

snip

Take a look at

http://perldoc.perl.org/perldata.html#Slices

and

http://perldoc.perl.org/perlref.html


Absolutely.

What's happening is that we assign what's returned from split() to a 
hash slice.


@hash{ @keys_list } = split;

The hash in this case is one of the anonymous hashes in %HoH. The 
reference to it, $HoH{$1}, needs to be dereferenced.


@{ $HoH{$1} }{ @keys_list } = split;
^

The keys_list is one of the anonymous arrays in %HoA. The reference to 
it, $HoA{$1}, needs to be dereferenced as well.


@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;
---^




say I have @data which below was pushed(bunch of them) with below hash 
of hash refernce


$VAR1 = {
 'element' = {
 'element2' = 'now',
 'element3' = '2',
 'element4' = 'serverxxx',
   }
   };

and then I would call sub do_it like
do_it(somevalue, $data)

sub do_it {
my $something = shift;
for (@_) {
  foreach my $XX ( keys %_ ) {
if ( exists ( $XX-{ element }{ element2 } )  $XX-{ element 
}{ element4} eq $something ) {
   for my $key ( keys %{ $XX{ element } } ) { -- 
Global symbol %XX requires explicit package name 
?

print $key\n;
print $XX and $XX{ element}{$key}\n;
   }
}
   }
}

why does it not like XX in above line ??

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




Re: how to replace @ inside of { }

2008-03-24 Thread Gunnar Hjalmarsson

Richard Lee wrote:
say I have @data which below was pushed(bunch of them) with below hash 
of hash refernce


$VAR1 = {
 'element' = {
 'element2' = 'now',
 'element3' = '2',
 'element4' = 'serverxxx',
   }
   };

and then I would call sub do_it like
do_it(somevalue, $data)

sub do_it {
my $something = shift;
for (@_) {
  foreach my $XX ( keys %_ ) {
if ( exists ( $XX-{ element }{ element2 } )  $XX-{ element 
}{ element4} eq $something ) {
   for my $key ( keys %{ $XX{ element } } ) { -- Global 
symbol %XX requires explicit package name ?

print $key\n;
print $XX and $XX{ element}{$key}\n;
   }
}
   }
}

why does it not like XX in above line ??


The above mix of English and Perl is confusing, and it's difficult to 
guess what you are trying to accomplish. You can't just introduce 
variables such as $data and %_, without showing how they were populated, 
and expect others to follow your line of thinking.


Please post a _complete_ program that we can copy and run, and that 
illustrates the issue you want help to resolve.


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: how to replace @ inside of { }

2008-03-24 Thread Richard Lee

Gunnar Hjalmarsson wrote:

Richard Lee wrote:
say I have @data which below was pushed(bunch of them) with below 
hash of hash refernce


$VAR1 = {
 'element' = {
 'element2' = 'now',
 'element3' = '2',
 'element4' = 'serverxxx',
   }
   };

and then I would call sub do_it like
do_it(somevalue, $data)

sub do_it {
my $something = shift;
for (@_) {
  foreach my $XX ( keys %_ ) {
if ( exists ( $XX-{ element }{ element2 } )  $XX-{ 
element }{ element4} eq $something ) {
   for my $key ( keys %{ $XX{ element } } ) { -- 
Global symbol %XX requires explicit package name 
?

print $key\n;
print $XX and $XX{ element}{$key}\n;
   }
}
   }
}

why does it not like XX in above line ??


The above mix of English and Perl is confusing, and it's difficult to 
guess what you are trying to accomplish. You can't just introduce 
variables such as $data and %_, without showing how they were 
populated, and expect others to follow your line of thinking.


Please post a _complete_ program that we can copy and run, and that 
illustrates the issue you want help to resolve.




below is hash of hash?
how do I dereference them (all of them)?



$VAR1 = {
 'number' = {
   'three' = '21',
   'five' = '23',
   'one' = 'number',
   'two' = '20',
   'four' = '22'
 }
   };
$VAR2 = {
 'alpha' = {
  'e' = '103',
  'c' = '101',
  'a' = 'alpha',
  'b' = '100',
  'd' = '102'
}
   };


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




Re: how to replace @ inside of { }

2008-03-24 Thread Gunnar Hjalmarsson

Richard Lee wrote:

below is hash of hash?


No, it's not. It's a failed attempt to try to imitate what's outputted 
when you use Data::Dumper to print a hash of hashes.



how do I dereference them (all of them)?


C:\hometype test.pl
my %HoH = (
'number' = {
'three' = '21',
'five' = '23',
'one' = 'number',
'two' = '20',
'four' = '22'
},
'alpha' = {
'e' = '103',
'c' = '101',
'a' = 'alpha',
'b' = '100',
'd' = '102'
}
);

# printing examples
print number - four is: $HoH{number}{four}\n;
print alpha - e is: $HoH{alpha}{e}\n;

C:\homeperl test.pl
number - four is: 22
alpha - e is: 103

C:\home

But you need to make an effort with the perl documentation.

perldoc perldsc
perldoc perlreftut
perldoc perlref

Good luck!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: how to replace @ inside of { }

2008-03-24 Thread Rob Dixon

Richard Lee wrote:


Gunnar Hjalmarsson wrote:


C:\hometype test.pl
use Data::Dumper;
my %HoA = (
something  = [ qw/val1 val2 val3 and so forth/ ],
something2 = [ qw/vala valb valc and so forth/ ],
something3 = [ qw/valZ valZ1 valZ2 so forth/ ],
);
my %HoH;
while ( DATA ) {
/^(\S+)/;
@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;
}
print Dumper \%HoH;

__DATA__
something3 one two three etc
something2 two three and so on
something one two three four five

C:\homeperl test.pl
$VAR1 = {
  'something3' = {
'so' = 'three',
'valZ2' = 'two',
'forth' = 'etc',
'valZ1' = 'one',
'valZ' = 'something3'
  },
  'something2' = {
'so' = 'so',
'valb' = 'two',
'forth' = 'on',
'valc' = 'three',
'vala' = 'something2',
'and' = 'and'
  },
  'something' = {
   'so' = 'four',
   'forth' = 'five',
   'val2' = 'one',
   'val1' = 'something',
   'val3' = 'two',
   'and' = 'three'
 }
};

C:\home


Hey, this works perfectly, thanks.

I just need to understand more on this

@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;

This is really spinning my heads in all direction trying to see if I can 
truly understand them instead of just using it.


Yes, it's not the most transparent line of code is it. The while loop is
equivalent to:

while ( DATA ) {
  my @values = split;
  my $key = $values[0];
  my $labels = $HoA{$key};
  my %map;
  @[EMAIL PROTECTED] = @values;
  $HoH{$key} = \%map;
}

And IMO it's better written like that. Does that help?

Rob



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




Re: how to replace @ inside of { }

2008-03-24 Thread Rob Dixon

Richard Lee wrote:


say I have @data which below was pushed(bunch of them) with below hash 
of hash refernce


$VAR1 = {
 'element' = {
 'element2' = 'now',
 'element3' = '2',
 'element4' = 'serverxxx',
   }
   };

and then I would call sub do_it like
do_it(somevalue, $data)

sub do_it {
my $something = shift;
for (@_) {
  foreach my $XX ( keys %_ ) {
if ( exists ( $XX-{ element }{ element2 } )  $XX-{ element 
}{ element4} eq $something ) {
   for my $key ( keys %{ $XX{ element } } ) { -- Global 
symbol %XX requires explicit package name ?

print $key\n;
print $XX and $XX{ element}{$key}\n;
   }
}
   }
}

why does it not like XX in above line ??


Hmm. You're getting very confused here.

The answer to your question is that you're referencing $XX{element}
which would be an element of hash %XX which doesn't exist.

But your code is wrong on many levels:

- Use either 'for' or 'foreach' but not both for the same purpose

- You're iterating over @_, which now contains just $data since you
  shifted 'something' off it

- There is no hash %_

- $XX should really be $xx if not something descriptive

- You use both $XX-{element} and $XX{element}. Which is right?

- Don't quote $something

- If $XX is a hash reference, then printing is nonsensical

And finally, and most importantly, you don't post the code that gave you
the output you describe. As it stands it won't compile because the
subroutine call has a bareword parameter and there is a mismatch of
braces in the subroutine definition.

If you would like help then you're more than welcome, but at least
please give us a chance and don't make us try to work out what the
question is before we offer an answer.

Rob

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




Re: how to replace @ inside of { }

2008-03-24 Thread Rob Dixon

Richard Lee 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?


Hi Richard.

You started this thread with the post above. Now you're asking:

Richard Lee wrote:

 below is hash of hash?
 how do I dereference them (all of them)?



 $VAR1 = {
  'number' = {
'three' = '21',
'five' = '23',
'one' = 'number',
'two' = '20',
'four' = '22'
  }
};
 $VAR2 = {
  'alpha' = {
   'e' = '103',
   'c' = '101',
   'a' = 'alpha',
   'b' = '100',
   'd' = '102'
 }
};

and I think all the posts in between haven't really helped very much as
you seem to be packing you input data into a hash structure and having
trouble unpacking it again.

If your method is wrong or unworkable then no amount of help to 
implement it will provide a solution. Furthermore,


May I ask you to please:

- Describe your input in as much detail as possible. Posting actual data
  would be ideal.

- Explain what result you want from that data

- Tell us what ideas you had about how to go about it. Real Perl code
  would be good, but please copy and paste rather than trying to
  describe what you have written

Then, I assure you, we will help you to the best of our abilities.

Rob

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




Re: how to replace @ inside of { }

2008-03-23 Thread Gunnar Hjalmarsson

Chas. Owens wrote:

On Sat, Mar 22, 2008 at 11:53 PM, Richard Lee [EMAIL PROTECTED] wrote:
snip

@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;

This is really spinning my heads in all direction trying to see if I can
truly understand them instead of just using it.

snip

Take a look at

http://perldoc.perl.org/perldata.html#Slices

and

http://perldoc.perl.org/perlref.html


Absolutely.

What's happening is that we assign what's returned from split() to a 
hash slice.


@hash{ @keys_list } = split;

The hash in this case is one of the anonymous hashes in %HoH. The 
reference to it, $HoH{$1}, needs to be dereferenced.


@{ $HoH{$1} }{ @keys_list } = split;
^

The keys_list is one of the anonymous arrays in %HoA. The reference to 
it, $HoA{$1}, needs to be dereferenced as well.


@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;
---^

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: how to replace @ inside of { }

2008-03-22 Thread Chas. Owens
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  = [qw1 2 3 4],
something2 = [qw110 11 12],
something3 = [qw20 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;
}
}


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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




Re: how to replace @ inside of { }

2008-03-22 Thread Richard Lee

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  = [qw1 2 3 4],
something2 = [qw110 11 12],
something3 = [qw20 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.

thank you.

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




Re: how to replace @ inside of { }

2008-03-22 Thread Rob Dixon

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  = [qw1 2 3 4],
something2 = [qw110 11 12],
something3 = [qw20 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'
};


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




Re: how to replace @ inside of { }

2008-03-22 Thread Gunnar Hjalmarsson

Richard Lee wrote:

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?


No, it's not very clear.

C:\hometype test.pl
use Data::Dumper;
my %HoA = (
something  = [ qw/val1 val2 val3 and so forth/ ],
something2 = [ qw/vala valb valc and so forth/ ],
something3 = [ qw/valZ valZ1 valZ2 so forth/ ],
);
my %HoH;
while ( DATA ) {
/^(\S+)/;
@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;
}
print Dumper \%HoH;

__DATA__
something3 one two three etc
something2 two three and so on
something one two three four five

C:\homeperl test.pl
$VAR1 = {
  'something3' = {
'so' = 'three',
'valZ2' = 'two',
'forth' = 'etc',
'valZ1' = 'one',
'valZ' = 'something3'
  },
  'something2' = {
'so' = 'so',
'valb' = 'two',
'forth' = 'on',
'valc' = 'three',
'vala' = 'something2',
'and' = 'and'
  },
  'something' = {
   'so' = 'four',
   'forth' = 'five',
   'val2' = 'one',
   'val1' = 'something',
   'val3' = 'two',
   'and' = 'three'
 }
};

C:\home

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Re: how to replace @ inside of { }

2008-03-22 Thread Richard Lee

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  = [qw1 2 3 4],
something2 = [qw110 11 12],
something3 = [qw20 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/




Re: how to replace @ inside of { }

2008-03-22 Thread Richard Lee

Gunnar Hjalmarsson wrote:

Richard Lee wrote:

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?


No, it's not very clear.

C:\hometype test.pl
use Data::Dumper;
my %HoA = (
something  = [ qw/val1 val2 val3 and so forth/ ],
something2 = [ qw/vala valb valc and so forth/ ],
something3 = [ qw/valZ valZ1 valZ2 so forth/ ],
);
my %HoH;
while ( DATA ) {
/^(\S+)/;
@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;
}
print Dumper \%HoH;

__DATA__
something3 one two three etc
something2 two three and so on
something one two three four five

C:\homeperl test.pl
$VAR1 = {
  'something3' = {
'so' = 'three',
'valZ2' = 'two',
'forth' = 'etc',
'valZ1' = 'one',
'valZ' = 'something3'
  },
  'something2' = {
'so' = 'so',
'valb' = 'two',
'forth' = 'on',
'valc' = 'three',
'vala' = 'something2',
'and' = 'and'
  },
  'something' = {
   'so' = 'four',
   'forth' = 'five',
   'val2' = 'one',
   'val1' = 'something',
   'val3' = 'two',
   'and' = 'three'
 }
};

C:\home


Hey, this works perfectly, thanks.

I just need to understand more on this

@{ $HoH{$1} }{ @{ $HoA{$1} } } = split;

This is really spinning my heads in all direction trying to see if I can 
truly understand them instead of just using it.


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




Re: how to replace @ inside of { }

2008-03-22 Thread Chas. Owens
On Sat, Mar 22, 2008 at 11:53 PM, Richard Lee [EMAIL PROTECTED] wrote:
snip
 @{ $HoH{$1} }{ @{ $HoA{$1} } } = split;

 This is really spinning my heads in all direction trying to see if I can
 truly understand them instead of just using it.
snip

Take a look at

http://perldoc.perl.org/perldata.html#Slices

and

http://perldoc.perl.org/perlref.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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




how to replace @ inside of { }

2008-03-21 Thread Richard Lee

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?

thanks.

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