Generate an associative array from a file

M

Mr_Noob

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
 
P

Peter Makholm

Mr_Noob said:
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
 
C

ccc31807

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
 
M

Mr_Noob

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 :

thanks in advance for ur help...

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..
 
C

ccc31807

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
 
M

Mr_Noob

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...
 
P

Peter Makholm

ccc31807 said:
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
 
C

ccc31807

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
 
C

ccc31807

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
 
M

Mr_Noob

thanks for your advise.
I am still struggling with hashref creation within a hash...could you
please give a small example ?
 
C

ccc31807

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
 
R

Ron Bergin

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.
 
C

ccc31807

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top