Solved

Set hash

Posted on 2000-03-25
6
162 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 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