Solved

Set hash

Posted on 2000-03-25
6
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Uploading Binary Data using Perl 5 99
.properties file to call function/method 9 63
How to generate square thumbnail using perl 13 90
parse a file and get data out 11 111
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

696 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