Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old February 27th, 2001, 08:03 AM
ais ais is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 34 ais User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 8 sec
Reputation Power: 8
Hi all,

Firstly I apologise if this has been asked, but I don't even know what to search for.

I have minimal understanding of perl and I need to make a change to an existing script.

If I have a field 'Code" with a value 'A1' I know how to display 'A1' as an associated name.

e.g.

%names ('A1' => 'A1 : Everything is OK')

and to print I would use :

$names{$rec{'Code'}}

Now the question how do I do the same thing

When the value of 'Code' is 'A1, A2' and I want to display :

A1 : Everything is OK
A2 : Caution is advised

So essentially I need to associated a comma delimited field of variable length (could any number codes) with display names.

Thanks in advance.
__________________

: ----- >>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<< ----- :
: ---- You can't judge a man by his clothes ...or from his name ---- :
: ------ But you can tell a lot about him ...from his signature !!! ------ :
: ----- >>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<< ----- :

Reply With Quote
  #2  
Old February 27th, 2001, 08:33 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Talking

I'm confused. The way you worded your question, it seems like the answer is to just add another key->value pair to %names.
Code:
%names ('A1' => 'A1 : Everything is OK',
        'A2' => 'A2 : Caution is advised' );

That way whatever 'Code' has a value of would be a key in %names.

Yet, you seem to be asking a different question.

Sorry, if I'm totally off base here.
__________________
- dsb -
Perl Guy

Reply With Quote
  #3  
Old February 27th, 2001, 08:55 AM
ais ais is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 34 ais User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 8 sec
Reputation Power: 8
Ok let me see if I can clarify more:

Let's say I have :

%names ('A1' => 'A1 : Everything is OK',
'A2' => 'A2 : Caution is advised' );


This will work with :

Code = 'A1' and will display 'A1 : Everything is OK'

or

Code = 'A2' and will diplay 'A2 : Caution is advised'


now how do I make

Code = 'A1, A2' display 'A1 : Everything is OK'
'A2 : Caution is advised'

In PHP I would explode the string and associate each register with $names, but I don't know how to do it in perl, although I am guessing something similar must be possible.


Reply With Quote
  #4  
Old February 27th, 2001, 09:01 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

Ok. I see.

Well you could split the comma delimited string on the comma.
Code:
@codes = split( /,/, $rec{Code} );

This would yield a list of codes.
If you wanted to use them to print line by line( or whatever ) you could run a loop through that array and print the values.
Code:
foreach $n ( 0 .. $#codes ) {
    print $name{$rec{Code}}, "\n";
}


Output would be something like:
Code:
$  perl script.pl
A1 : Everything is OK
A2 : Caution is advised
$  


Hope that helps.

Reply With Quote
  #5  
Old February 27th, 2001, 09:13 AM
ais ais is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 34 ais User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 8 sec
Reputation Power: 8
Thanks dsb, I think that looks about like what I need.

Reply With Quote
  #6  
Old February 27th, 2001, 09:18 AM
ais ais is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 34 ais User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 8 sec
Reputation Power: 8
Sorry just one more point, I would have to split on a comma and a space, do I just type a space after the comma or do I need to enclose it in quotes like in PHP?

Thanks in advance.

Reply With Quote
  #7  
Old February 27th, 2001, 02:37 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Talking

As long as its between the 2 '/'s then you'll be good. I would use "\s" though. It'd just be easier to see when you're glancing over the code.

Code:
@codes = split( /,\s/, $rec{Code} );


Also, I just realized a mistake I made. In my last post I said a loop like this would work:
Code:
foreach $n ( 0 .. $#codes ) {
    print $name{$rec{Code}}, "\n";
}

That would give the original line you split though. Do this instead:
Code:
foreach $n ( 0 .. $#codes ) {
    print $name{$codes[$n]}, "\n";
}


Sorry, for the confusion.



Reply With Quote
  #8  
Old February 28th, 2001, 07:23 PM
ais ais is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 34 ais User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 8 sec
Reputation Power: 8
Thanks for the update. I'll try that.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Associating a value with a display name


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway