Solved

Set hash

Posted on 2000-03-25
6
165 Views
Last Modified: 2010-03-05
Hello,

I have the following file:

8|bg_color|#FFFFFF #006699 #EEEEEE #FFFFFF||Specify background colors.
8|font_color|#000000 #000066 #000066 #000066||Specify font colors.
8|font_face|verdana verdana verdana verdana,sans-serif||Specify font face.
8|font_size|3 2 2 2|-4 -3 -2 -1 0 1 2 3 4 +1 +2 +3 +4|Specify font size.

Whats I want to do is to make a hash out of each line.
For example:

Line one will become:

%bg_color = (
      0 => "#FFFFFF",
      1 => "#006699",
      2 => "#EEEEEE",
      3 => "#FFFFFF"
);

I tried something like this, but it did not work!
First read the input file into file @$r_rows.
Then try to make the hashes:

      foreach (@$r_rows) {
            chomp;
            ($mode,$key,$value) = split /\|/;
            ${$key} = $value;
            $r_setup{$key} = $value;
            if ($mode eq "8") {
                  @layout = split(/\ /, $row[2]);            
                  my $j = -1;
                  foreach (@layout) {
                        $j++;
                        if ($key eq "bg_color") {
                              $bg_color{$j} = $_;
                        }
                        elsif ($key eq "font_color") {
                              $font_color{$j} = $value;
                        }
                        elsif ($key eq "font_face") {
                              $font_face{$j} = $value;
                        }
                        elsif ($key eq "font_size") {
                              $font_size{$j} = $value;
                        }
                  }
            }

Can someone help me??
0
Comment
Question by:mmcw
6 Comments
 
LVL 1

Expert Comment

by:lambda
ID: 2657126
I haven't tried out the code, but I think I see a problem... U have used two foreach () loops, one inside the other. And the variables are read into the default variable, $_ both the times. Try reading them into different variables.

foreach $line (@$r_rows) {
.....
.....
foreach $l_line (@layout) {
....



0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2657677
This is how you need to do it:

@$r_rows = ("8|bg_color|#FFFFFF #006699 #EEEEEE #FFFFFF||Specify background colors.
", "8|font_color|#000000 #000066 #000066 #000066||Specify font colors.
", "8|font_face|verdana verdana verdana verdana,sans-serif||Specify font face.
", "8|font_size|3 2 2 2|-4 -3 -2 -1 0 1 2 3 4 +1 +2 +3 +4|Specify font size.
");

foreach (@$r_rows) {
   chomp;
   ($mode,$key,$valueString) = split /\|/;
   @Values = split ' ', $valueString;
   my $j = 0;
   foreach $value (@Values) {
      ${$key}{$j} = $value;
      $j++;
   }
}

print "Value of \%bg_color key 0 is $bg_color{0}\n";
print "Value of \%bg_color key 1 is $bg_color{1}\n";
print "Value of \%bg_color key 2 is $bg_color{2}\n";
print "Value of \%bg_color key 3 is $bg_color{3}\n\n";
print "Value of \%font_color key 1 is $font_color{1}\n\n";
print "Value of \%font_face key 3 is $font_face{3}\n\n";
print "Value of \%font_size key 0 is $font_size{0}\n";
print "Value of \%font_size key 1 is $font_size{1}\n";
print "Value of \%font_size key 2 is $font_size{2}\n";
print "Value of \%font_size key 3 is $font_size{3}\n";


This is what it prints out, which seems to be what you want.  (Did you mean to have no space in front of sans-serif in your data?)

Value of %bg_color key 0 is #FFFFFF
Value of %bg_color key 1 is #006699
Value of %bg_color key 2 is #EEEEEE
Value of %bg_color key 3 is #FFFFFF

Value of %font_color key 1 is #000066

Value of %font_face key 3 is verdana,sans-serif

Value of %font_size key 0 is 3
Value of %font_size key 1 is 2
Value of %font_size key 2 is 2
Value of %font_size key 3 is 2

0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2657685
I can't help wondering why you want hashes.  It would seem simpler to use arrays.  Then
   $bg_color[0] would be #FFFFFF,
   $bg_color[1] would be #006699, etc.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 2657791
my $j = 0;
 foreach $value (@Values) {
           ${$key}{$j} = $value;
           $j++;
 }
#or
@{$key}{0..$#Values} = @Values;

#but as PC_User321 says, why not
$key{$key} = [split ' ', $valueString];
#or
@{$key} = split ' ', $valueString;
0
 

Expert Comment

by:perlman
ID: 2663206
Hi.

I think that is a too technical way to store layout. Why don't you use stylesheets, a .css-file:

body { background-color: #FFFFFF }
..text { font-color: #000000; font-family: Verdana; font-size: 10px }
..text2 { font-color: #000000; font-family: Verdana; font-size: 10px }

and then you could use different files in you html-document:
"<LINK REL=stylesheet CONTENT=text/css HREF=style$a.css>"

and/or around a text:
"<SPAN CLASS=text$a>text</SPAN>"

I prefer to just load different html-templates-files which contains the styles...

0
 

Expert Comment

by:perlman
ID: 2663223
not two dots (just one) before the text.
(stylesheets only works in ver 4-browsers, allmost)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question