Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Too much Hash??

by HamNRye (Monk)
on Jun 02, 2004 at 20:57 UTC ( #359745=perlquestion: print w/replies, xml ) Need Help??
HamNRye has asked for the wisdom of the Perl Monks concerning the following question:


I'm sure this is something stupid, but I've been staring at this code and trying things for hours on this mess.

There's obviously something wrong with my assignment to the hash on line 21, but it looks OK to me. It looks like $SubOption is being treated as a Hash reference.

As always, help is appreciated.

#! /usr/bin/perl -w use strict; my %data; %data = ReadConfig("legacy.ini"); foreach my $value (values %data){ print "$value\n"; } print "$data{MAIN}\n\n"; sub ReadConfig { my $ConfigFile=shift; my %Config; my $Item; my $Value; open CONFIG,"<$ConfigFile" or die("Cannot open Config File!"); while(<CONFIG>) { chomp; next if ((/^#/)||(/^\s*$/)); if (/^\[(.*)\]$/) {$Item=$1;next;}; if($Item) { my ($SubOption,$Value)=split /\=/; if ($Value) { print "$Item\t$SubOption\t$Value\t"; $Config{$Item}{$SubOption} = $Value; print "-->$Config{$Item}{$SubOption}\n"; } else { $Config{$Item} = $_; } } } close CONFIG; foreach my $value (keys %Config){ print "->$value\n"; } return %Config; }

Here's the ini file....

[MAIN] inputDir = "\\\\richnas1\\newsdata\\brides" tempDir = C:\\Temp textExt = .txt imageExt = PF.tif logfile = C:\\Temp\\brides.log [FTP] hostname = username = USERNAME password = PASSWORD imagedir = /timesdispatch/Celebrations/Photos/ textdir = /timesdispatch/Celebrations/ bridescap = /files1/BRIDESCAP [E-Mail] smtphost = [Notify]

And the output....

MAIN inputDir "\\\\richnas1\\newsdata\\brides" --> "\ +\\\richnas1\\newsdata\\brides" MAIN tempDir C:\\Temp --> C:\\Temp MAIN textExt .txt --> .txt MAIN imageExt PF.tif --> PF.tif MAIN logfile C:\\Temp\\brides.log --> C:\\Temp\\brides.l +og FTP hostname --> FTP username USERNAME --> USERNAME FTP password PASSWORD --> PASSWORD FTP imagedir /timesdispatch/Celebrations/Photos/ --> /t +imesdispatch/Celebrations/Phot os/ FTP textdir /timesdispatch/Celebrations/ --> /timesdisp +atch/Celebrations/ FTP bridescap /files1/BRIDESCAP --> /files1/BRIDESCAP E-Mail smtphost --> inet-mail1 ->MAIN ->FTP ->Notify ->E-Mail HASH(0x1b8f138) HASH(0x1b854dc) HASH(0x1b8559c) HASH(0x1b8f138)

Edit by castaway, removed what appear to be real username/passwords


Yep, it was something stupid I was doing. When the split was happening it was leaving a trailing space on the SubOptions. I would also need to clear that up for possible indents... So I was getting $data{MAIN}{'inputDir '} Once I printed out the entire structure, it jumped right out at me. Thanks to all those who helped, sorry I wasn't more clear on the problem.

Replies are listed 'Best First'.
Re: Too much Hash??
by Roy Johnson (Monsignor) on Jun 02, 2004 at 21:12 UTC
    What would you want the output to be? %data is indexed by $Item and each value will be a reference to a hash indexed by $SubOption. My guess is you want your printout loop to be something along these lines:
    foreach my $k (keys %data){ print "$k:\n"; print " $_\n" for (values %{$data{$k}}); }

    The PerlMonk tr/// Advocate
Re: Too much Hash??
by robartes (Priest) on Jun 02, 2004 at 21:15 UTC
    It looks like $SubOption is being treated as a Hash reference.

    No, but $Config{$Item} is. You're printing the values of the %Config hash, which are either hashrefs (in the case of sections with foo=bar lines) or scalars (in other cases). Nothing to be surprised at.

    BTW, you'll be running into trouble when you have stanzas in your ini file which contain multiple lines without = in them (e.g. multiple email addresses in [Notify]). You might want to consider changing your data structure.


      Nothing to be surprised at.

      Well, it obviously suprised me. Thanks for telling me that this is normal behaviour, and I've done something stupid.

      Is that a syntax screw-up on my part then?? In my perl books, the closest syntax I can find is $hash{$key}{'string'} = $value, and that's pretty much what I've always used. I thought maybe the backslashes in the path were what was causing it. I've tried puting quotes around the vars (both single and double), escaping said quotes, and a few different syntaxes using the -> and => operators, etc.... I'm talking desperation stuff.

      Could someone at least point me to something that will give my desperation some focus??

      Here is an old write-up of mine that uses the same $MAIN::PageInfo{$section}{$pnum} = $_; style of putting things into a hash....

      Is this much like the last write-up where something is falling out of scope?


      From perldsc -

      # reading from file # flintstones: lead=fred pal=barney wife=wilma pet=dino while ( <> ) { next unless s/^(.*?):\s*//; $who = $1; for $field ( split ) { ($key, $value) = split /=/, $field; $HoH{$who}{$key} = $value; }

      That looks like roughly what I have, without the variables localized. I see I initialize $SubOption at the split, but even with that moved to the top of the sub it still fails.

        What do you mean by "fails?" It would be helpful if you showed us what your expected output is and where the actual output differs. To me it looks like your code is working just fine. To get a better sense of what's actually going on, you might try replacing

        foreach my $value (values %data){ print "$value\n"; }

        with something more like

        foreach my $value (values %data) { if (ref $value ne "HASH") { print "$value\n"; } else { print "$_\n" foreach values %$value; } }

        The point being, that when creating your main hash, some of your values are strings, others are hash references. That's the way your code is designed, so you'll need to change your handling routines to accomodate it.

Re: Too much Hash??
by tadamec (Beadle) on Jun 02, 2004 at 21:20 UTC

    Edit your post and remove the username/passwords.

    Then change them on the server.

Re: Too much Hash??
by tadamec (Beadle) on Jun 02, 2004 at 21:16 UTC
      Yes, I've seen it, I'm just trying to keep from using any non-standard modules. Makes it alot easier for maintanance.
        I'm just trying to keep from using any non-standard modules. Makes it alot easier for maintanance.
        Errr...come again? So, you're saying that you'd rather throw away the thousands-of-eyes testing for a CPAN module to bypass having to type 'perl -MCPAN -eshell' at the command line? To each his own, I suppose...


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://359745]
Approved by Limbic~Region
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-07-21 16:01 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (449 votes). Check out past polls.