Pb of key in a table

A

Alextophi

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

How to post only the keys with their value ?

thank you, Christophe
 
P

Paul Lalli

Alextophi said:
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
 
D

Dave Weaver

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.
 
A

Anno Siegel

Dave Weaver said:
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
 
P

Paul Lalli

Dave said:
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
 
A

Alextophi

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
 
G

Gunnar Hjalmarsson

Alextophi said:
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";
}
 
W

William James

Alextophi said:
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.
 
B

Bob Walton

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.
 

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

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top