|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 !!! ------ : : ----- >>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<< ----- : |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
Thanks dsb, I think that looks about like what I need.
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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. ![]() |
|
#8
|
|||
|
|||
|
Thanks for the update. I'll try that.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Associating a value with a display name |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|