comments in the middle of a hash variable ?

N

nadsinoz

I have a hash variable that I would like to modify each time the script
is run by commenting out fields that I am not interesed in. How can I
make this work?


%fields = (
lastname => $fieldnum++,
firstname => $fieldnum++,
# middlename => $fieldnum++,
userid => $fieldnum++,
rscname => $fieldnum++,
email => $fieldnum++,
roleid => $fieldnum++,
)

Thanks in advance,

Chris
 
P

Paul Lalli

nadsinoz said:
I have a hash variable that I would like to modify each time the script
is run by commenting out fields that I am not interesed in. How can I
make this work?


%fields = (
lastname => $fieldnum++,
firstname => $fieldnum++,
# middlename => $fieldnum++,
userid => $fieldnum++,
rscname => $fieldnum++,
email => $fieldnum++,
roleid => $fieldnum++,
)

Uhm. Can you explain how the above is *not* working for you?

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $fieldnum = 0;

my %fields = (
lastname => $fieldnum++,
firstname => $fieldnum++,
# middlename => $fieldnum++,
userid => $fieldnum++,
rscname => $fieldnum++,
email => $fieldnum++,
roleid => $fieldnum++,
) ;

print Dumper(\%fields);

__END__

$VAR1 = {
'rscname' => '3',
'userid' => '2',
'lastname' => 0,
'firstname' => '1',
'email' => '4',
'roleid' => '5'
};


Paul Lalli
 
X

xhoster

nadsinoz said:
I have a hash variable that I would like to modify each time the script
is run by commenting out fields that I am not interesed in. How can I
make this work?

You can make it work by commenting out fields you are not interested in
each time you run the script.
%fields = (
lastname => $fieldnum++,
firstname => $fieldnum++,
# middlename => $fieldnum++,
userid => $fieldnum++,
rscname => $fieldnum++,
email => $fieldnum++,
roleid => $fieldnum++,
)

Yep, just like that.

Xho
 
A

attn.steven.kuo

nadsinoz said:
I have a hash variable that I would like to modify each time the script
is run by commenting out fields that I am not interesed in. How can I
make this work?


%fields = (
lastname => $fieldnum++,
firstname => $fieldnum++,
# middlename => $fieldnum++,
userid => $fieldnum++,
rscname => $fieldnum++,
email => $fieldnum++,
roleid => $fieldnum++,
)


I don't understand your question. Do you want
data persistance and to delete keys with repeated
invocations of your script?

If so, consider the Storable module:

perldoc Storable
perldoc -f delete

Do you want a source filter?
Perhaps the Filter::Simple module
from CPAN would do?
 
J

Joe Smith

nadsinoz said:
I have a hash variable that I would like to modify
each time the script is run

That's reasonable.
by commenting out fields that I am not interesed in.

But that is not.
How can I make this work?

%fields = (
lastname => $fieldnum++,
firstname => $fieldnum++,
# middlename => $fieldnum++,
userid => $fieldnum++,
rscname => $fieldnum++,
email => $fieldnum++,
roleid => $fieldnum++,
)

Remove the '#' and add some code to remove the keys you don't
want. That is, change the hash dynamically at runtime instead
of constantly editing the source code.

delete $fields{middlename} if $ignore_middle_name;

-Joe
 
T

Thomas Kratz

Joe said:
That's reasonable.



But that is not.



Remove the '#' and add some code to remove the keys you don't
want. That is, change the hash dynamically at runtime instead
of constantly editing the source code.

delete $fields{middlename} if $ignore_middle_name;

Wouldn't it be easier not to create the value in the first place?

%fields = (
$do_lastname ? (lastname => $fieldnum++) : (),
$do_firstname ? (firstname => $fieldnum++) : (),
...
)

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
R

Raphael Wegmann

Paul said:
Uhm. Can you explain how the above is *not* working for you?

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $fieldnum = 0;

my %fields = (
lastname => $fieldnum++,
firstname => $fieldnum++,
# middlename => $fieldnum++,
userid => $fieldnum++,
rscname => $fieldnum++,
email => $fieldnum++,
roleid => $fieldnum++,
) ;

print Dumper(\%fields);

__END__

$VAR1 = {
'rscname' => '3',
'userid' => '2',
'lastname' => 0,
'firstname' => '1',
'email' => '4',
'roleid' => '5'
};

I'd guess, he wants
'userid' => 3,
'rscname' => 4,
....

so Joe's "delete $fields{middlename} if $ignore_middle_name;"
seems the way to go.

lG.
 
P

Paul Lalli

Raphael said:
I'd guess, he wants
'userid' => 3,
'rscname' => 4,
...

Wow, I wish *I* had the same psychic abilities you do!

The OP did not say what results he wanted. You and Joe are both
guessing, just as I am. Until the OP mentions what he wants, there's
no way of knowing what is "correct".

Paul Lalli
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top