Click here to get back home

Generate an associative array from a file

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Generate an associative array from a file Mr_Noob 02-28-2008
Posted by Mr_Noob on February 28, 2008, 8:59 am
Please log in for more thread options
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


Posted by Peter Makholm on February 28, 2008, 9:06 am
Please log in for more thread options

> 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

Posted by ccc31807 on February 28, 2008, 9:56 am
Please log in for more thread options
> 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
if $hash
($key, $value) = split on /=/ #create vars
create hashref like $hash = $value

close INFILE

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

exit


Posted by Mr_Noob on February 28, 2008, 1:20 pm
Please log in for more thread options
>
>
>
> > Hi all,
>
> > here is a sample of my file :
>
> > ## blah blah
> > [client1]
> > remote=3D192.168.1.2
> > ### some comments here
> > ### blah blah
> > [client2]
> > remote=3D192.168.1.5
> > passive=3Dtrue
> > ###blablah
> > [client3]
> > remote=3D192.168.1.8
> > [client4]
> > remote=3D192.168.1.15
> > passive=3Dtrue
> > ###
> > ####
> > ####
>
> > 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 $_ =3D~ /#/ #skip comments
> if $_ =3D~ /[/ #create hash element
> create hash element like $hash
> if $hash
> ($key, $value) =3D split on /=3D/ #create vars
> create hashref like $hash =3D $value
>
> close INFILE
>
> foreach my $client (sort keys %hash)
> foreach my $key (sort keys %})
> print $hash - $hash
>
> 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..


Posted by ccc31807 on February 28, 2008, 3:30 pm
Please log in for more thread options

> 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


Similar ThreadsPosted
Re: how to generate an associative array and count frequency? December 25, 2007, 6:13 pm
Help: Reading file and creating an associative array August 21, 2008, 3:50 pm
associative array help June 2, 2006, 12:31 pm
Getting associative array from a hash July 7, 2005, 7:10 pm
extracting records to an associative array September 13, 2005, 3:41 pm
Quick Associative Array Question December 5, 2005, 11:23 am
Perl CGI does not generate file. January 13, 2005, 2:29 pm
Difference between accessing arrays and associative arrays using a int index April 12, 2006, 12:58 am
Generate png thumbnail October 30, 2005, 11:51 pm
Generate Squential Numbers? December 6, 2004, 10:34 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap