barazani has asked for the wisdom of the Perl Monks concerning the following question:

Hi to All,
i need a little glue and a fresh pot of coffee .
i'm a sysadmin and not a coder so at some point i just cant
figure something out.
here's the thing :
i have a file called hostdb

#cat hostdb
hostname:host1 ip:192.168.1.1 cpu:PIII other info:fjfjfjfjfjjf ::: hostname:host2 ip:192.168.1.2 cpu:PIII other info:fjfjfjfjfjjf ::: hostname:host2 ip:192.168.1.2 cpu:PIII other info:fjfjfjfjfjjf :::
-----------------------------------
this is the proccessing script:
#cat hostdb_proc.pl
#!/usr/bin/perl -w use diagnostics; $/ = ":::\n"; # define ::: as paragraph delimiter #figure out how many paragraphs are there ? open(HOSTDB,"hostdb") or die "cannot open hostdb"; $record = 0; while(<HOSTDB>){ $record++; } print "number of records - $record\n"; close(HOSTDB); open(HOSTDB,"hostdb") or die "cannot open hostdb"; $host1 = <HOSTDB>; $host2 = <HOSTDB> ; $host3 = <HOSTDB>; close(HOSTDB); chomp($host1); chomp($host2); chomp($host3); #this is just for me to see what's been proccessed ... #print "paragraph1 = $host1"; #print "--------------------------\n"; #print "paragraph2 = $host1 "; #print "--------------------------\n"; #print "paragraph3 = $host1 "; #print "--------------------------\n"; foreach( $host_record = 1 ; $host_record <= $record ; $host_record++ ) +{ print "------host.$host_record--------\n"; $field = 0; for $field (split /\n/, ${"host".$host_record}) { print "Field contains: `$field'\n"; } }

the part i cant figure out is the $host(number) part in
which i assign the paragraph to a variable.
currently it's manual but i need it to be dynamic based on
the number of records.
any idea's ?
on the same note is it a waste that i open/close the file
twice ? can it be done in one time ?
thanks
barazani

Replies are listed 'Best First'.
Re: proccessing file paragraphs -braindead i am ..
by Joost (Canon) on Dec 13, 2002 at 15:04 UTC
    I can see you are used to shell programming :-)

    You're probably better off using an array:

    $/ = ":::\n"; # define ::: as paragraph delimiter open(HOSTDB,"hostdb") or die "cannot open hostdb: $!"; while (<HOSTDB>) { chomp; push @records,map( { { split/:/ } } split/\n/ ); # updated to make + hashrefs.... close HOSTDB; my $count = 0; for my $rec (@records) { print "record $count\n"; print "hostname: $rec->{hostname}\n"; print "ip: $rec->{ip}\n"; $count++; }
    Which also eliminates the need for counting the records in advance..

    By the way; if you are not using the info anywhere else, you could also just print it as you go, instead of storing the records in the array.

    Update: Added extra { } to map.

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Thank you both. and yes I am used to shell scripting , dont know how to shake that one off ..... barazani
Re: proccessing file paragraphs -braindead i am ..
by Chady (Priest) on Dec 13, 2002 at 14:59 UTC

    you can use a simple array to hold the hosts, as in:

    open(HOSTDB,"hostdb") or die "cannot open hostdb"; # open hosts one time only. my @hosts; # array to hold the hosts. while(<HOSTDB>){ push @hosts, chomp($_); } # using scalar on an array will give you the number of elements print "number of records - ", scalar @hosts, "\n"; close(HOSTDB); # instead of counting and selecting the hosts, use perl's foreach # to loop inside the @hosts array foreach my $host (@hosts){ print "------host.$host--------\n"; for my $field (split /\n/, $host ) { print "Field contains: '$field'\n"; } }

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/