comments in the middle of a hash variable ?

Discussion in 'Perl Misc' started by nadsinoz, Jul 21, 2005.

  1. nadsinoz

    nadsinoz Guest

    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
     
    nadsinoz, Jul 21, 2005
    #1
    1. Advertising

  2. nadsinoz

    Paul Lalli Guest

    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::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
     
    Paul Lalli, Jul 21, 2005
    #2
    1. Advertising

  3. nadsinoz

    Guest

    "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?


    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
     
    , Jul 21, 2005
    #3
  4. nadsinoz

    Guest

    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
     
    , Jul 21, 2005
    #4
  5. nadsinoz

    Joe Smith Guest

    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
     
    Joe Smith, Jul 22, 2005
    #5
  6. nadsinoz

    Thomas Kratz Guest

    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^.-
     
    Thomas Kratz, Jul 22, 2005
    #6
  7. Paul Lalli schrieb:
    >
    > 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.
    --
    Raphael
     
    Raphael Wegmann, Jul 22, 2005
    #7
  8. nadsinoz

    Paul Lalli Guest

    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
     
    Paul Lalli, Jul 23, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,157
  2. Monk
    Replies:
    10
    Views:
    1,506
    Michael Wojcik
    Apr 20, 2005
  3. rp
    Replies:
    1
    Views:
    557
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    644
    David A. Black
    Jul 2, 2008
  5. Replies:
    4
    Views:
    635
    Dr John Stockton
    Jun 3, 2006
Loading...

Share This Page