Solved

Set hash

Posted on 2000-03-25
6
166 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl, group, sort, count question 6 140
pattern matching in perl 2 105
Perl efficient DB Call 8 93
Search in text file in column and compare 4 53
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 …
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 …
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…

840 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