Pb of key in a table

Discussion in 'Perl Misc' started by Alextophi, Sep 20, 2005.

  1. Alextophi

    Alextophi Guest

    hello,
    I have a table, but certain keys not of value.

    How to post only the keys with their value ?

    thank you, Christophe
     
    Alextophi, Sep 20, 2005
    #1
    1. Advertising

  2. Alextophi

    Paul Lalli Guest

    Alextophi wrote:
    > I have a table, but certain keys not of value.


    What is a table? I'm assuming you mean a hash.

    > How to post only the keys with their value ?


    I am making the assumption that you have a hash in which only some of
    the keys have a defined value, and that you want to print only those
    keys whose value is defined.

    If this is not the case, please post example input and desired output.
    And please read the posting guidelines for this group.

    for my $key (grep {defined} keys %hash){
    print "$key ==> $hash{$key}\n";
    }

    for more information:
    perldoc -f grep
    perldoc -f defined
    perldoc -f keys

    Paul Lalli
     
    Paul Lalli, Sep 20, 2005
    #2
    1. Advertising

  3. Alextophi

    Dave Weaver Guest

    On 20 Sep 2005 05:02:16 -0700, Paul Lalli <> wrote:
    > I am making the assumption that you have a hash in which only some of
    > the keys have a defined value,


    How can a hash key be undefined?
    I've experimented, but I can't manage to generate a hash with a key
    that's not defined.
     
    Dave Weaver, Sep 20, 2005
    #3
  4. Alextophi

    Anno Siegel Guest

    Dave Weaver <> wrote in comp.lang.perl.misc:
    > On 20 Sep 2005 05:02:16 -0700, Paul Lalli <> wrote:
    > > I am making the assumption that you have a hash in which only some of
    > > the keys have a defined value,

    >
    > How can a hash key be undefined?


    You are perfectly right, it can't.

    Paul certainly meant to write something like (also untested)

    print "$_ => $hash{ $_}\n" for grep defined $hash{ $_}, keys %hash;

    One could also clean up the hash once and for all:

    defined $hash{ $_} or delete $hash{ $_} for keys %hash;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Sep 20, 2005
    #4
  5. Alextophi

    Paul Lalli Guest

    Dave Weaver wrote:
    > On 20 Sep 2005 05:02:16 -0700, Paul Lalli <> wrote:
    > > I am making the assumption that you have a hash in which only some of
    > > the keys have a defined value,

    >
    > How can a hash key be undefined?
    > I've experimented, but I can't manage to generate a hash with a key
    > that's not defined.


    Whoops. As Anno stated, yes, I obviously meant to check the hash
    *value* for defined'ness, not the keys.

    My apologies to the OP for the misleading incorrect code.

    Paul Lalli
     
    Paul Lalli, Sep 20, 2005
    #5
  6. Alextophi

    Alextophi Guest

    I rest my question correctly.

    I have a table: @SCHED, it contains 8 fields: $SCHED[0] $SCHED[1]
    $SCHED[2] $SCHED[3] $SCHED[4] $SCHED[5] $SCHED[6] $SCHED[7]

    my problem is that certain fields are empty and I have errors of
    initialization of variable.

    - How to make a print of $SCHED[0] to [7]

    in advance thank you for your assistance
    Christophe
     
    Alextophi, Sep 20, 2005
    #6
  7. Alextophi wrote:
    > I have a table: @SCHED, it contains 8 fields: $SCHED[0] $SCHED[1]
    > $SCHED[2] $SCHED[3] $SCHED[4] $SCHED[5] $SCHED[6] $SCHED[7]
    >
    > my problem is that certain fields are empty and I have errors of
    > initialization of variable.
    >
    > - How to make a print of $SCHED[0] to [7]


    for ( 0..$#SCHED ) {
    print "[$_] = ".(defined $SCHED[$_] ? $SCHED[$_] : '')."\n";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 20, 2005
    #7
  8. Alextophi wrote:
    > I rest my question correctly.
    >
    > I have a table: @SCHED, it contains 8 fields: $SCHED[0] $SCHED[1]
    > $SCHED[2] $SCHED[3] $SCHED[4] $SCHED[5] $SCHED[6] $SCHED[7]
    >
    > my problem is that certain fields are empty and I have errors of
    > initialization of variable.
    >
    > - How to make a print of $SCHED[0] to [7]
    >
    > in advance thank you for your assistance
    > Christophe


    I think that you would find it more enjoyable to use Ruby.

    =====
    names = [ 'Christophe', 'William', nil, 'Elaine', 'Gunnar the Troll' ]
    puts names
    puts "-"
    puts names.compact
    =====
    Output:

    Christophe
    William
    nil
    Elaine
    Gunnar the Troll
    -
    Christophe
    William
    Elaine
    Gunnar the Troll


    If you need the indices:
    =====
    names.each_with_index { |s,i| puts "#{i}. #{s ? s : ''}" }
    =====
    Output:

    0. Christophe
    1. William
    2.
    3. Elaine
    4. Gunnar the Troll


    If you want only the names of humans:
    =====
    names.each_with_index { |s,i| s=nil if s && s.index("Troll")
    puts "#{i}. #{s ? s : ''}" }
    =====
    Output:

    0. Christophe
    1. William
    2.
    3. Elaine
    4.
     
    William James, Sep 20, 2005
    #8
  9. Alextophi

    Bob Walton Guest

    Alextophi wrote:

    ....
    > I have a table: @SCHED, it contains 8 fields: $SCHED[0] $SCHED[1]
    > $SCHED[2] $SCHED[3] $SCHED[4] $SCHED[5] $SCHED[6] $SCHED[7]
    >
    > my problem is that certain fields are empty and I have errors of
    > initialization of variable.
    >
    > - How to make a print of $SCHED[0] to [7]

    ....
    > Christophe
    >


    Debugging prints of arrays or hashes are done nicely and easily
    with the Data::Dumper module. Like:

    use Data::Dumper;
    #code creating array @SCHED...
    print Dumper(\@SCHED);

    for example. Doing it that way, you don't have to know what is
    stored in the array or hash -- it will print it regardless. And
    the nice printout it generates is executable Perl code which will
    regenerate the entity.

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Sep 21, 2005
    #9
    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. David Williams
    Replies:
    2
    Views:
    1,147
    Jacob Yang [MSFT]
    Aug 12, 2003
  2. Christian Bongiorno

    custom key and hasmap using a ranged key

    Christian Bongiorno, Jun 15, 2004, in forum: Java
    Replies:
    1
    Views:
    466
    Roedy Green
    Jun 15, 2004
  3. Rio
    Replies:
    4
    Views:
    1,220
  4. Patrick Guio
    Replies:
    6
    Views:
    3,230
    chris
    Oct 20, 2004
  5. M P
    Replies:
    1
    Views:
    502
Loading...

Share This Page