Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > comments in the middle of a hash variable ?

Reply
Thread Tools

comments in the middle of a hash variable ?

 
 
nadsinoz
Guest
Posts: n/a
 
      07-21-2005
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-21-2005
nadsinoz wrote:
> 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:umper;
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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      07-21-2005
"nadsinoz" <(E-Mail Removed)> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      07-21-2005
nadsinoz wrote:
> 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?

--
Hope this helps,
Steven

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-22-2005
nadsinoz wrote:
> 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
 
Reply With Quote
 
Thomas Kratz
Guest
Posts: n/a
 
      07-22-2005
Joe Smith wrote:
> nadsinoz wrote:
>
>> 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;


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^.-
 
Reply With Quote
 
Raphael Wegmann
Guest
Posts: n/a
 
      07-22-2005
Paul Lalli schrieb:
>
> Uhm. Can you explain how the above is *not* working for you?
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Data:umper;
> 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.
--
Raphael
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      07-22-2005
Raphael Wegmann wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Variable declarations at the middle of a block works Infinity C Programming 9 01-05-2010 11:47 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Variable declaration in middle of block? seamoon C Programming 7 03-01-2005 03:55 PM



Advertisments