Hash's Regex Transformation with Map

E

Edward WIJAYA

Hi all,

How can I make my snippet below:


$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'

To produce:

$VAR1 = {
'ASGG 3' => 'foo',
'CTGS 2' => 'bar',
};

Principally I want to change the key of the old hash by
replacing bracketed strings with "S".
 
G

Gunnar Hjalmarsson

Edward said:
How can I make my snippet below:


$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'

To produce:

$VAR1 = {
'ASGG 3' => 'foo',
'CTGS 2' => 'bar',
};

For a start, you'd better ask Perl for help:

$ perl -wMData::Dumper -e ...
------------^

Personally, when the output from a code snippet differs from what you
had expected, I don't think that one-liners should be posted here. By
writing the program in a file and enabling strictures, there is a good
chance that you find your mistake.
 
R

robic0

Hi all,

How can I make my snippet below:


$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'

To produce:

$VAR1 = {
'ASGG 3' => 'foo',
'CTGS 2' => 'bar',
};

Principally I want to change the key of the old hash by
replacing bracketed strings with "S".

Have to copy $_ in the map statement otherwise $h gets altered.
I would not create a variable to anonymous reference this way
unless it is going to be passed to subroutines alot or creating
a class. Reserve anonymous array/hash references for array elements.
Just my opinion.....

use strict;
use warnings;
use Data::Dumper;

#my $h = {'A[TCG]GG 3' => 'foo', 'CTG[AA] 2' => 'bar'};
#my %nh = map { my $k=$_; $k =~s/\[[ATCG]+\]/S/g; $k=> $h->{$_} }
(keys %{$h});

my %h = ('A[TCG]GG 3' => 'foo', 'CTG[AA] 2' => 'bar');
my %nh = map { my $k=$_; $k =~s/\[[ATCG]+\]/S/g; $k=> $h{$_} } (keys
%h);

print Dumper \%nh;
#print Dumper $h;
print Dumper \%h;

------------------------
output:

$VAR1 = {
'ASGG 3' => 'foo',
'CTGS 2' => 'bar'
};
$VAR1 = {
'CTG[AA] 2' => 'bar',
'A[TCG]GG 3' => 'foo'
};
 
G

Glenn Jackman

At 2005-11-25 10:49PM said:
Hi all,

How can I make my snippet below:


$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'

$k is undefined

%nh = map { ($k = $_) =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
-------------------^^^^^
 
R

robic0

At 2005-11-25 10:49PM said:
Hi all,

How can I make my snippet below:


$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'

$k is undefined

%nh = map { ($k = $_) =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
^^
Yeah but get the can't use global in "my ($k = $_)" since $_ is
global and declared elsewhere (if strict).
 
T

Tad McClellan

robic0 said:
At 2005-11-25 10:49PM said:
Hi all,

How can I make my snippet below:


$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'

$k is undefined

%nh = map { ($k = $_) =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
^^
Yeah but get the can't use global in "my ($k = $_)"


Did you try it?

Did in not work when you did?

since $_ is
global and declared elsewhere (if strict).


The built-in variables need not be declared, even under "use strict".
 
G

Glenn Jackman

At 2005-11-26 08:30AM said:
%nh = map { ($k = $_) =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
^^
Yeah but get the can't use global in "my ($k = $_)" since $_ is
global and declared elsewhere (if strict).

You want "(my $k = $_)" then.
 
R

robic0

At 2005-11-26 08:30AM said:
%nh = map { ($k = $_) =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
^^
Yeah but get the can't use global in "my ($k = $_)" since $_ is
global and declared elsewhere (if strict).

You want "(my $k = $_)" then.

Ahhh so thats it. I found this an odd error since apparently the
"=" sign passed the syntax check before the global.
So the construct "my ($k = $z) =~ s/aaaa/asdf/g" would be ok then?
It also seems odd that multiple single value assignments would cause
a error also: "my $k=$z=0;" or I remember it gives me errors.
 
T

Tad McClellan

So the construct "my ($k = $z) =~ s/aaaa/asdf/g" would be ok then?


What happened when you tried it?

It also seems odd that multiple single value assignments would cause
a error also: "my $k=$z=0;" or I remember it gives me errors.


It would be a strict message, because you have not declared $z.
 
A

Anno Siegel

Tad McClellan said:
robic0 said:
Hi all,

How can I make my snippet below:


$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'

$k is undefined

%nh = map { ($k = $_) =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
^^
Yeah but get the can't use global in "my ($k = $_)"


Did you try it?

Did in not work when you did?

my ( $k = $_) =~ ...

does indeed complain about use of global $_ in "my".

( my $k = $_) =~ ...

works.

Anno
 
R

robic0

Tad McClellan said:
robic0 said:
On 26 Nov 2005 12:47:57 GMT, Glenn Jackman <[email protected]>
wrote:

Hi all,

How can I make my snippet below:


$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'

$k is undefined

%nh = map { ($k = $_) =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
^^
Yeah but get the can't use global in "my ($k = $_)"


Did you try it?

Did in not work when you did?

my ( $k = $_) =~ ...

does indeed complain about use of global $_ in "my".

( my $k = $_) =~ ...

works.

Anno
hehe...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top