Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: How do you move within an array using foreach?

by P0w3rK!d (Pilgrim)
on Oct 10, 2002 at 18:48 UTC ( [id://204279]=note: print w/replies, xml ) Need Help??


in reply to Re: How do you move within an array using foreach?
in thread How do you move within an array using foreach?

Ovid,
There are actually n number of tokens in the file. I was trying to simplify the question. :)

As I read each section, I need to drop each of these into a hash.
For example:

# there are n number of files to parse %masterhash = ( %file_1_hash, ..., %file_n_hash); # within each file hash %file_1_hash = (%foo, %foo2); #within each section %foo = ( letter => "a", number => "b" ); # %letter would = a # %number would = b ... with n number of elements within foohash # same for foo2 %foo2 = ( %letter, %number); # same as foo #... Note: n number of sections in each file.
Does that make sense?

-P0w3rK!d :)

Replies are listed 'Best First'.
Re (3): How do you move within an array using foreach?
by VSarkiss (Monsignor) on Oct 10, 2002 at 19:15 UTC

    By "section", are you referring to .ini file sections? If so, do you know about Config::IniFiles? I haven't used it, but it's a tied-hash interface that looks like it does what you want.

    If you are dealing with .ini files, but this isn't exactly the right thing, poke around CPAN; there's a lot of modules for dealing with them, so you can probably find one that does whatever you need.

      Thank you! :>

      This will save me a ton of time. The file is actually an install shield file.

Re: Re: Re: How do you move within an array using foreach?
by P0w3rK!d (Pilgrim) on Oct 10, 2002 at 18:51 UTC
    # please forgive me..there are the hashes.. %foo1 = ( a => "1", b => "2", c => "3", ); %foo2 = ( d => "4", e => "5", f => "6", );
      How about something like this then:
      #! /usr/bin/perl -w use strict; use Data::Dumper; my @lines = (<>); my %data = (); my $token = ''; foreach (@lines) { if (/\[(\w+)\]/) { $token = $1; } if (/(\w+)=(\w+)/) { $data{$token}->{$1} = $2; } } print Dumper(\%data);
      The output is:
      $VAR1 = { 'foo2' => { 'e' => '5', 'f' => '6', 'd' => '4' }, 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3' } };
      Not 100% what you want - this creates a hash of hashes - but still useable.

      JJ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://204279]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-28 18:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found