Generate an associative array from a file

Discussion in 'Perl Misc' started by Mr_Noob, Feb 28, 2008.

  1. Mr_Noob

    Mr_Noob Guest

    Hi all,

    here is a sample of my file :


    ## blah blah
    [client1]
    remote=192.168.1.2
    ### some comments here
    ### blah blah
    [client2]
    remote=192.168.1.5
    passive=true
    ###blablah
    [client3]
    remote=192.168.1.8
    [client4]
    remote=192.168.1.15
    passive=true
    ###
    ####
    ####

    I am trying to write a perl script that would exclude all comments
    from the above file and then generate an associative array, and output
    the following result :


    client1;192.168.1.1
    client2;192.168.1.5;true
    client3;192.168.1.8
    client4;192.168.1.15;true
    ....

    thanks in advance for ur help...

    br
     
    Mr_Noob, Feb 28, 2008
    #1
    1. Advertising

  2. Mr_Noob <> writes:

    > I am trying to write a perl script that would exclude all comments
    > from the above file and then generate an associative array, and output
    > the following result :


    Looks like something that is parsable by Config::INI, Config::Simple,
    Config::Tiny or probaly a couple of other existing modules.

    //Makholm
     
    Peter Makholm, Feb 28, 2008
    #2
    1. Advertising

  3. Mr_Noob

    ccc31807 Guest

    On Feb 28, 8:59 am, Mr_Noob <> wrote:
    > Hi all,
    >
    > here is a sample of my file :
    >
    > ## blah blah
    > [client1]
    > remote=192.168.1.2
    > ### some comments here
    > ### blah blah
    > [client2]
    > remote=192.168.1.5
    > passive=true
    > ###blablah
    > [client3]
    > remote=192.168.1.8
    > [client4]
    > remote=192.168.1.15
    > passive=true
    > ###
    > ####
    > ####
    >
    > I am trying to write a perl script that would exclude all comments
    > from the above file and then generate an associative array, and output
    > the following result :
    >
    > client1;192.168.1.1
    > client2;192.168.1.5;true
    > client3;192.168.1.8
    > client4;192.168.1.15;true
    > ...
    >
    > thanks in advance for ur help...
    >
    > br


    Here is some pseudocode:

    my %hash
    open INFILE, <file.csv

    while <INFILE>
    next if $_ =~ /#/ #skip comments
    if $_ =~ /[/ #create hash element
    create hash element like $hash{client1}
    if $hash{client1}
    ($key, $value) = split on /=/ #create vars
    create hashref like $hash{client}{$key} = $value

    close INFILE

    foreach my $client (sort keys %hash)
    foreach my $key (sort keys %{$hash{$client}})
    print $hash{$client} - $hash{$client}{$key}

    exit
     
    ccc31807, Feb 28, 2008
    #3
  4. Mr_Noob

    Mr_Noob Guest

    On 28 fév, 15:56, ccc31807 <> wrote:
    > On Feb 28, 8:59 am, Mr_Noob <> wrote:
    >
    >
    >
    > > Hi all,

    >
    > > here is a sample of my file :

    >
    > > ## blah blah
    > > [client1]
    > > remote=192.168.1.2
    > > ### some comments here
    > > ### blah blah
    > > [client2]
    > > remote=192.168.1.5
    > > passive=true
    > > ###blablah
    > > [client3]
    > > remote=192.168.1.8
    > > [client4]
    > > remote=192.168.1.15
    > > passive=true
    > > ###
    > > ####
    > > ####

    >
    > > I am trying to write a perl script that would exclude all comments
    > > from the above file and then generate an associative array, and output
    > > the following result :

    >
    > > client1;192.168.1.1
    > > client2;192.168.1.5;true
    > > client3;192.168.1.8
    > > client4;192.168.1.15;true
    > > ...

    >
    > > thanks in advance for ur help...

    >
    > > br

    >
    > Here is some pseudocode:
    >
    > my %hash
    > open INFILE, <file.csv
    >
    > while <INFILE>
    > next if $_ =~ /#/ #skip comments
    > if $_ =~ /[/ #create hash element
    > create hash element like $hash{client1}
    > if $hash{client1}
    > ($key, $value) = split on /=/ #create vars
    > create hashref like $hash{client}{$key} = $value
    >
    > close INFILE
    >
    > foreach my $client (sort keys %hash)
    > foreach my $key (sort keys %{$hash{$client}})
    > print $hash{$client} - $hash{$client}{$key}
    >
    > exit


    Thank you for these answers. The problem is that what's inside the
    brackets won't always start with the "client"...It can be any string..
    I'll also have a look on Config::INI, Config::Simple, Config::Tiny..
     
    Mr_Noob, Feb 28, 2008
    #4
  5. Mr_Noob

    ccc31807 Guest

    On Feb 28, 12:20 pm, Mr_Noob <> wrote:

    > Thank you for these answers. The problem is that what's inside the
    > brackets won't always start with the "client"...It can be any string..
    > I'll also have a look on Config::INI, Config::Simple, Config::Tiny..


    You missed the point. Perl is the Practical Extraction and Reporting
    Language. All you are doing os Practically Extracting and Reporting!
    This is simply a data transformation task, reading data from one
    document, manipulating it, and printing it out to another document.
    This is what Perl does, for goodness sake!

    Follow this recipe:
    1. open the in file.
    2. open the out file.
    3. while the in file is open, read each line, manipulate it, and
    print it to the out file.
    4. close the in file.
    5. close the out file.

    No need for any fancy modules, just the native functions.

    CC
     
    ccc31807, Feb 28, 2008
    #5
  6. Mr_Noob

    Mr_Noob Guest

    Oook,

    Here is what i succeed to write so far :

    my %hash;
    open(INFILE, $cftp_conf_file) or die "$cftp_conf_file : $!";

    while (<INFILE>)
    {
    next if $_ =~ /^\#/;
    if ($_ =~ /^\[/)
    {
    $hash{cftp_client_name} = $_;
    }
    }
    close (INFILE);
    print %hash;


    this only creates a list of clients ... but I cannot find how to
    retrieve other client's info (lines beginning with "remote" and
    "passive") and feed the array with it...
     
    Mr_Noob, Feb 29, 2008
    #6
  7. ccc31807 <> writes:

    > This is what Perl does, for goodness sake!


    Forcing you to reinvent existing wheel? But why oh why?

    To me, Perl is the exact opposite...

    //Makholm
     
    Peter Makholm, Feb 29, 2008
    #7
  8. Mr_Noob

    ccc31807 Guest

    On Feb 29, 8:56 am, Mr_Noob <> wrote:
    > Oook,
    >
    > Here is what i succeed to write so far :
    >
    > my %hash;
    > open(INFILE, $cftp_conf_file) or die "$cftp_conf_file : $!";
    >
    > while (<INFILE>)
    > {
    > next if $_ =~ /^\#/;
    > if ($_ =~ /^\[/)
    > {
    > $hash{cftp_client_name} = $_;
    > }
    > }
    > close (INFILE);
    > print %hash;
    >
    > this only creates a list of clients ... but I cannot find how to
    > retrieve other client's info (lines beginning with "remote" and
    > "passive") and feed the array with it...


    My approach would be to create a hash of hashrefs. This is an approach
    I have found useful in my job, and I do a LOT of the kind of stuff you
    want to do with your input file. Randal Schwartz wrote a little book
    about learning Perl objects, modules, and references, and there is
    code in there that you can adapt almost verbatim. I shamelessly copied
    his code the same way I am recommending to you.

    Okay, it's tough at first, I'll grant, but it does get a lot easier
    over time, and if you do this kind of stuff often, you'll soon find
    that you can bash out a script from memory -- which is quick and
    (repeating myself) very useful.

    CC
     
    ccc31807, Feb 29, 2008
    #8
  9. Mr_Noob

    ccc31807 Guest

    On Feb 29, 8:58 am, Peter Makholm <> wrote:
    > ccc31807 <> writes:
    > > This is what Perl does, for goodness sake!

    >
    > Forcing you to reinvent existing wheel? But why oh why?


    No, not reinventing the wheel. Just using the wheel for its intended
    purpose.

    Actually, writing a module to do what Perl will natively do anyway
    seems reinventing the wheel to me. If you can do something directly
    with a function, or indirectly with a module that might call the
    function or duplicate its functionality, why not do it directly?

    CC
     
    ccc31807, Feb 29, 2008
    #9
  10. Mr_Noob

    Mr_Noob Guest

    thanks for your advise.
    I am still struggling with hashref creation within a hash...could you
    please give a small example ?
     
    Mr_Noob, Feb 29, 2008
    #10
  11. Mr_Noob

    ccc31807 Guest

    On Feb 29, 11:50 am, Mr_Noob <> wrote:
    > thanks for your advise.
    > I am still struggling with hashref creation within a hash...could you
    > please give a small example ?


    Look at your Perl documentation. Look at these man pages:
    perldsc - data structures cookbook
    perlreftut = Perl reference tutor
    perllol - Perl lists of lists

    A reference is a scalar value that contains a memory address, a scalar
    value, that points to another place in memory that may contain
    anything else, including a hash. Look at the examples in the man pages
    given above.

    Bottom line: You need to learn how to do this for yourself. I can't
    give you an example script to follow that would work for you. I could
    give you an example script that works for me, but that won't do you
    any good. You need to learn how to do this for yourself.

    CC
     
    ccc31807, Feb 29, 2008
    #11
  12. Mr_Noob

    Ron Bergin Guest

    On Feb 29, 7:28 am, ccc31807 <> wrote:
    > On Feb 29, 8:58 am, Peter Makholm <> wrote:
    >
    > > ccc31807 <> writes:
    > > > This is what Perl does, for goodness sake!

    >
    > > Forcing you to reinvent existing wheel? But why oh why?

    >
    > No, not reinventing the wheel. Just using the wheel for its intended
    > purpose.
    >
    > Actually, writing a module to do what Perl will natively do anyway
    > seems reinventing the wheel to me. If you can do something directly
    > with a function, or indirectly with a module that might call the
    > function or duplicate its functionality, why not do it directly?
    >
    > CC


    If you follow that logic, then you're also saying that a very large
    percentage of the modules on cpan are reinventing the wheel and should
    not be used. To me, that seems like pretty flaky logic. It sounds
    like you don't believe in the idea of code reuse.
     
    Ron Bergin, Feb 29, 2008
    #12
  13. Mr_Noob

    ccc31807 Guest

    On Feb 29, 3:27 pm, Ron Bergin <> wrote:
    > > Actually, writing a module to do what Perl will natively do anyway
    > > seems reinventing the wheel to me. If you can do something directly
    > > with a function, or indirectly with a module that might call the
    > > function or duplicate its functionality, why not do it directly?

    >
    > > CC

    >
    > If you follow that logic, then you're also saying that a very large
    > percentage of the modules on cpan are reinventing the wheel and should
    > not be used. To me, that seems like pretty flaky logic. It sounds
    > like you don't believe in the idea of code reuse.


    Not at all. Just the opposite. When you have code that does what you
    want and has been tested and used by others, use it. When you have
    code that doesn't do what you want, that hasn't been tested and used,
    don't use it.

    All things being equal, it's much better to use a built-in function
    that has been proven effective over a period of years than it is to
    use a module that hasn't been proven effective and that attempts to
    duplicate the functionality of the in built function.

    Why is advice to use the built in Perl functions an indication that I
    don't believe in code reuse? Seems just the opposite to me.

    (I'm not offering an opinion on the effectiveness or safety of any
    module, just saying that you don't need to overlook the capabilities
    of the language simply because some module purports to do what you
    want.)

    CC
     
    ccc31807, Feb 29, 2008
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. desktop
    Replies:
    5
    Views:
    396
    James Kanze
    Jun 26, 2007
  2. Yvon Thoraval
    Replies:
    5
    Views:
    216
    Jason Creighton
    Sep 17, 2003
  3. a
    Replies:
    4
    Views:
    110
    Jürgen Exner
    Dec 26, 2007
  4. VK
    Replies:
    47
    Views:
    564
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    665
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page