Solved

Set hash

Posted on 2000-03-25
6
163 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use perl to insert into MySQL database 9 128
HTTP Error 502.2 - Bad Gateway 3 205
Port 80 requests 16 87
PHP pack() : What exactly is meant by signed char and unsigned char? 7 96
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now