Re: simple hash question

2004-11-08 Thread John W. Krahn
Ing. Branislav Gerzo wrote:
Hi pals,
Hello,
I have simple hash question, lets we have example code:
my %hash = ( 1 => 2, 2 => 3, 3 => 4, 4 => 5 );
while (my($key, $value) = each(%hash)) { print "$key => $value\n"; }
how I can print all values, if I want print that in this order:
2 => 3
4 => 5
3 => 4
1 => 2
You could store the keys you want in the order you want in an array.
my @keys = ( 2, 4, 3, 1 );
print "$_ => $hash{$_}\n" for @keys;

John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Re: simple hash question

2004-11-08 Thread Ing. Branislav Gerzo
Edward Wijaya [EW], on Tuesday, November 09, 2004 at 03:14 (+0800)
wrote:

EW> use Tie::IxHash module from CPAN, to preserve order in a Hash.

thanks for answer, indeed, I created small subroutine, to look...

sub hash_look {
while ( my ($key, $value) = each(%tab_def) ) {
print "$key $value, " if $key =~ /$_[0]/i;
}
}

-- 

 ...m8s, cu l8r, Brano.

[I wouldn't be paranoid if everyone didn't pick on me.]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: simple hash question

2004-11-08 Thread Edward Wijaya
use Tie::IxHash module from CPAN, to preserve order in a Hash.
Regards,
Edward WIJAYA
SINGAPORE
On Tue, 09 Nov 2004 03:06:03 +0800, Edward Wijaya <[EMAIL PROTECTED]>  
wrote:

On Mon, 8 Nov 2004 11:59:13 +0100, Ing. Branislav Gerzo <[EMAIL PROTECTED]>  
wrote:

Hi pals,
I have simple hash question, lets we have example code:
my %hash = ( 1 => 2, 2 => 3, 3 => 4, 4 => 5 );
while (my($key, $value) = each(%hash)) { print "$key => $value\n"; }
how I can print all values, if I want print that in this order:
2 => 3
4 => 5
3 => 4
1 => 2
thanks for any ideas.
--
 --. ,--  ,- ICQ: 7552083  \|||/`//EB: www.2ge.us
,--' |  - |--IRC: [2ge](. .),\\SN: 2ge!2ge_us
`+==+=+===~  ~=-o00-(_)-00o-~
Second childhood?  I haven't finished the first one.





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Re: simple hash question

2004-11-08 Thread Edward Wijaya
On Mon, 8 Nov 2004 11:59:13 +0100, Ing. Branislav Gerzo <[EMAIL PROTECTED]>  
wrote:

Hi pals,
I have simple hash question, lets we have example code:
my %hash = ( 1 => 2, 2 => 3, 3 => 4, 4 => 5 );
while (my($key, $value) = each(%hash)) { print "$key => $value\n"; }
how I can print all values, if I want print that in this order:
2 => 3
4 => 5
3 => 4
1 => 2
thanks for any ideas.
--
 --. ,--  ,- ICQ: 7552083  \|||/`//EB: www.2ge.us
,--' |  - |--IRC: [2ge](. .),\\SN: 2ge!2ge_us
`+==+=+===~  ~=-o00-(_)-00o-~
Second childhood?  I haven't finished the first one.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



simple hash question

2004-11-08 Thread Ing. Branislav Gerzo
Hi pals,

I have simple hash question, lets we have example code:

my %hash = ( 1 => 2, 2 => 3, 3 => 4, 4 => 5 );
while (my($key, $value) = each(%hash)) { print "$key => $value\n"; }

how I can print all values, if I want print that in this order:
2 => 3
4 => 5
3 => 4
1 => 2

thanks for any ideas.

--

 --. ,--  ,- ICQ: 7552083  \|||/`//EB: www.2ge.us
,--' |  - |--IRC: [2ge](. .),\\SN: 2ge!2ge_us
`+==+=+===~  ~=-o00-(_)-00o-~
Second childhood?  I haven't finished the first one.
 




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Simple Hash Question / NEVER MIND

2003-12-31 Thread Jeff Westman
DOH   .
DOH   .
NEVER MIND, user error ( <- gun to head)
DOH   .
DOH   .
DOH   .





Hi 

I have a hash defined as

my %flag3 = ( A => "DBSpace backup thread",
  B => "Begin work",
  C => "Commiting/committed",
  H => "Heuristic aborting/aborted",
  P => "Preparing/prepared",
  R => "Aborting/aborted",
  X => "XA prepare" );

I want to print out the value of the corresponding key -- without a loop
iteration.  The following works (displays 'value exists'), but is not what I
want

print "value exists" if exists $flag3{$pos3};

I want this to print the string.

HELP PLEASE!

TIA


--
Jeff


__
Do you Yahoo!?
Find out what made the Top Yahoo! Searches of 2003
http://search.yahoo.com/top2003

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Simple Hash Question

2003-12-31 Thread Jeff Westman
Hi 

I have a hash defined as

my %flag3 = ( A => "DBSpace backup thread",
  B => "Begin work",
  C => "Commiting/committed",
  H => "Heuristic aborting/aborted",
  P => "Preparing/prepared",
  R => "Aborting/aborted",
  X => "XA prepare" );

I want to print out the value of the corresponding key -- without a loop
iteration.  The following works (displays 'value exists'), but is not what I
want

print "value exists" if exists $flag3{$pos3};

I want this to print the string.

HELP PLEASE!

TIA


--
Jeff


__
Do you Yahoo!?
Find out what made the Top Yahoo! Searches of 2003
http://search.yahoo.com/top2003

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Probably a simple hash question

2003-01-24 Thread Michael Hooten
> Here's the setup:
> I have set up a hash %categories where the key is one number and the value
> is another
> i.e. $categories('1094') = 100049-0220-14

I assume you meant (note braces and quotes):

  $categories{1094} = '100049-0220-14'

otherwise the value will be 100049-0220-14 = -99.

> So, further on down, I'm trying to do a lookup in %categories to see if a
> value is there.  (I can
> swap the key and the value in the setup if it will make this next step
> easier)
> and I had a while loop to search through each of the keys & values, but
> the problem is
> that it doesn't break out of the while loop, and the wrong value gets set
> as $myID;

There are two ways I can think of: (1) you can copy and reverse the hash or
(2) you can loop through the values until you find a match.


--
# remember, hashes are not in key order
# note () NOT {}
%categories = ( 1094 => '100049-0220-14',
1095 => '100059-5220-54',
1096 => '100669-6620-660004',
1097 => '100779-7720-770004',
  );
$value = '100779-7720-770004';

# (1)
%reversed = reverse %categories;
print "$reversed{$value}\n";

# (2)
foreach $key(keys %categories){
   print "Loop ", ++$x, "\n";
   if($categories{$key} eq $value){
  print "$key\n";
  print "Exiting within loop $x\n";
  last;
   }
}

--

> #until ($value == $ccid)

Data are text not numeric ($value eq $ccid). See above.


>   #}## Need to break out of the while loop here once find it.

last;

> So, here's the question:
> Where can I find information on how to use variables as the keys in a
> hash?

I hope this answers your question. If %categories is not that large OR if
you have a fast computer OR if you have a lotta memory, I would opt for (1)
above because there is no looping--ie, less code to maintain, that is always
good. If you no longer need the original %categories, you can also do

   %categories = reverse %categories;

Otherwise, opt for (2).



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Probably a simple hash question

2003-01-23 Thread R. Joseph Newton
"Lara J. Fabans" wrote:

> Hi, perl-friends,...

Hi Lara,

You said:

> I had a while loop to search through each of the keys & values, but the problem is
> that it doesn't break out of the while loop, and the wrong value gets set as $myID;

Look at your words, thenlook at your code:

> #while(my ($key, $value) = each %categories )

Since you know that one of your stopping ocnditions is to have the value == $ccid, AND 
you also know that prudence demands that you check to make sure that your hash line 
returns a value before you try to use that value, the and operator is called for:
while(my ($key, $value) = each %categories  and !($value == $ccid)) {

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Probably a simple hash question

2003-01-23 Thread Dan Muey


> Morning, Dan

Morning!

> 
> At 11:50 AM 1/23/2003 -0600, you wrote:
> > > i.e. $categories('1094') = 100049-0220-14
> >
> >  do you mean $catagories{'1094'} ?
> 
> Yup, I did.  Never type anything in from memory :-)

I can't anyway because my memory doesn't work.

> 
> 
> > > value is there.  (I can
> >
> >if(exists $catagories{$ccid}) {  }

Also if it would help you could do :

if(!exists $catagories{$ccid}) {  }
Which is saying if the key $ccid does *not* exist in the hash catagories then { ...}
Were as the first one was saying
If the key does exist in the hash ctatgories the {  }

> 
> Coolness.  So then, if I can talk out loud a second,
> to get the value out, I should be able to just say something 
> like $myID = $categories{$ccid}, right? It was returning 

Right, or instead of :
$myID = $categories{$ccid};
print $myID;
You could just do :
print $categories{$ccid};
Or otherwse just use $categories{$ccid} where you would want to use $myID
And save a step or two.

Isn't perl fun?!

> NULL, but I may have swapped out the key with the value when 
> I was initially setting it up.
> 
> 
> Have a most excellent day
> lara
> -
> Lara J. Fabans
> Lodestone Software, Inc
> [EMAIL PROTECTED]
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Probably a simple hash question

2003-01-23 Thread Rob Dixon
Lara J. Fabans wrote:
> Hi, perl-friends,
>
> Here's the setup:
> I have set up a hash %categories where the key is one number and the
> value is another
> i.e. $categories('1094') = 100049-0220-14

You need quotation marks here, like this:

$categories('1094') = '100049-0220-14';

otherwise you're setting the hash value to -99!

>
> So, further on down, I'm trying to do a lookup in %categories to see
> if a value is there.  (I can
> swap the key and the value in the setup if it will make this next step
> easier)
> and I had a while loop to search through each of the keys & values,
> but the problem is
> that it doesn't break out of the while loop, and the wrong value gets
> set as $myID;
>
> $ccid = 1094;
>
> ## Attempt #2
> ### 1.22.03 change to try to get it to stop once it finds
> ###   the correct value.
> #until ($value == $ccid)
> # {
> # ($key, $value) = each %categories;
> # }
>
> ## Attempt #1
> #while(my ($key, $value) = each %categories )
> #{
> #print "CC ID: $ccid: Key : $key : Value: $value\n";
> #if ($value == $ccid)
> # {
> #$myID = $key;
>   #}## Need to break out of the while loop here once find it.
> #}
>
> print "My  ID is $categories{$ccid}\n";  ## Returns a null

Hi Lara.

I think you're misunderstanding hashes. They work this way round:

my (%hash, $key, $value);
$key = 1094;
$value = '100049-0220-14'

$hash{$key} = $value;

so it looks like you're searching the hash for an entry
where $value == 1094, which doesn't exist. Also, this is
exactly what hashes were meant for so you don't need the
loop to find an entry with the given key. Just write:

$value = $hash{$ccid};

and it's done.

I hope I've understood what you're trying to do.

Cheers,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Probably a simple hash question

2003-01-23 Thread Dan Muey
> Hi, perl-friends,
> 
> Here's the setup:
> I have set up a hash %categories where the key is one number 
> and the value 
> is another
> i.e. $categories('1094') = 100049-0220-14

 do you mean $catagories{'1094'} ?
> 
> So, further on down, I'm trying to do a lookup in %categories 
> to see if a 
> value is there.  (I can

if(exists $catagories{$ccid}) {  }
You can do that anywhere after the hash is created and it will work peachy!
As far as the while not stopping why not try a foreach

foreach $k(keys %catagories) {  }

> swap the key and the value in the setup if it will make this 
> next step 
> easier)
> and I had a while loop to search through each of the keys & 
> values, but the 
> problem is
> that it doesn't break out of the while loop, and the wrong 
> value gets set 
> as $myID;
> 
> $ccid = 1094;
> 
> ## Attempt #2
> ### 1.22.03 change to try to get it to stop once it finds
> ###   the correct value.
> #until ($value == $ccid)
> # {
> # ($key, $value) = each %categories;
> # }
> 
> ## Attempt #1
> #while(my ($key, $value) = each %categories )
> #{
> #print "CC ID: $ccid: Key : $key : Value: $value\n";
> #if ($value == $ccid)
> # {
> #$myID = $key;
>   #}## Need to break out of the while loop here once find it.
> #}
> 
> print "My  ID is $categories{$ccid}\n";  ## Returns a null
> 
> Unfortunately, in all the documentation, it shows using an 
> HTG value in the hash ($categories('1094')), and I only have 
> a variable to access. I looked into mapping, but got my head 
> wrapped around the axel.
> 
> So, here's the question:
> Where can I find information on how to use variables as the 
> keys in a hash?
> 
> Thanks,
> Lara
> -
> Lara J. Fabans
> Lodestone Software, Inc
> [EMAIL PROTECTED]
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Probably a simple hash question

2003-01-23 Thread Lara J. Fabans
Hi, perl-friends,

Here's the setup:
I have set up a hash %categories where the key is one number and the value 
is another
i.e. $categories('1094') = 100049-0220-14

So, further on down, I'm trying to do a lookup in %categories to see if a 
value is there.  (I can
swap the key and the value in the setup if it will make this next step 
easier)
and I had a while loop to search through each of the keys & values, but the 
problem is
that it doesn't break out of the while loop, and the wrong value gets set 
as $myID;

$ccid = 1094;

## Attempt #2
### 1.22.03 change to try to get it to stop once it finds
###   the correct value.
#until ($value == $ccid)
# {
# ($key, $value) = each %categories;
# }

## Attempt #1
#while(my ($key, $value) = each %categories )
#{
#print "CC ID: $ccid: Key : $key : Value: $value\n";
#if ($value == $ccid)
# {
#$myID = $key;
 #}## Need to break out of the while loop here once find it.
#}

print "My  ID is $categories{$ccid}\n";  ## Returns a null

Unfortunately, in all the documentation, it shows using an HTG value in the
hash ($categories('1094')), and I only have a variable to access.
I looked into mapping, but got my head wrapped around the axel.

So, here's the question:
Where can I find information on how to use variables as the keys in a hash?

Thanks,
Lara
-
Lara J. Fabans
Lodestone Software, Inc
[EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]