snafu has asked for the wisdom of the Perl Monks concerning the following question:
237 : AMAFILE: AMA210 238 : STATUS: NETWORK 239 : DATE CREATED: 9/20/01 240 : 241 : AMAFILE: AMA211 242 : STATUS: NETWORK 243 : DATE CREATED: 9/20/01 244 : 245 : AMAFILE: AMA212 246 : STATUS: NETWORK 247 : DATE CREATED: 9/20/01 248 : 249 : AMAFILE: AMA213 250 : STATUS: CLOSED 251 : DATE CREATED: 9/20/01 252 : 253 : AMAFILE: AMA214 254 : STATUS: OPEN 255 : DATE CREATED: 9/20/01
Desired:
I would like each block (here seperated by the empty space after each colon) to be a hash within an array. <readmore>
Therefore:
$myarray[253] = ( $myhash{AMAFILE} = "AMA214"; $myhash{STATUS} = "OPEN"; $myhash{DATE CREATED} = "9/20/01");
My difficulty here is that I am doing this whole thing in a map{} where each element of $_ is each line (the counter above indicates each $_). Now, I have my ideas of how to do this. That is not the problem. I am curious to see what others may think the *best* way to do this is.
One way I could do this is a brute force attack using my counter where I could push() a temporary array for 3 iterations of the map{} loop so that I could get my data for my one iteration of my array of hashes. Then split that temporary array up into my hashes and assign those hashes to my main array of hashes. I hope that made sense. This is the direction Im leaning toward going but if anything is apparent in Perl there is always a cleaner, easier way of doing things. So, before I "waste" my time on a down and dirty implication of this I thought I would post this to the rest of the monk kingdom.
Here is my code so far:
#!/usr/local/bin/perl -w use strict; use File::Basename; use Env; my (%SITREP, $param, $value, @SORTME); my $script = "$HOME/DSC/bin/checkDisplayBatch"; my $base = basename($script); my $c = 0; eval { $SIG{ALRM} = sub { print "Timeout during operation!\n"; exit(5); }; }; open(CDB,"$script |"); # Set a timeout value so that it doesn't hang! alarm(30); while ( <CDB> ) { chomp; die("$base failed probably due to the X25 link being saturated rig +ht now.\n". "Try again in a moment...". "Dump from $script: $!\n\n") if ( /ERROR/ ); next if ( /-/ ); if ( /AMA/ or /STATUS/ ) { push(@SORTME,split(/\|/)); } } # So far this is the beginning of my implementation # of what I want to do. This is the code (partly # tweaked now) that gave the above output of data # that I am working with. This, in other words, is # where my work, I think, needs to be worked on more. # There is some code in here that I have just placed # to test and I know it will not totally work. Just # disregard that, please ;) map { $_ =~ s/\s//; # print $c++ ." : $_\n"; <-- obviously this is the code # that gave us the above output if ( /\w/ ) { ($param,$value) = split(/:/); $value =~ s/\s+//g; $array[$c] = ($SITREP{$param} = $value); if ( $value eq "CLOSED" ) { print ""; } } else { $c++; } } @SORTME;
----------
- Jim
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Suggestions on how to make array of hashes with this...
by merlyn (Sage) on Sep 21, 2001 at 02:42 UTC | |
by snafu (Chaplain) on Sep 21, 2001 at 09:37 UTC | |
|
Re: Suggestions on how to make array of hashes with this...
by blakem (Monsignor) on Sep 21, 2001 at 02:51 UTC | |
by snafu (Chaplain) on Sep 21, 2001 at 09:39 UTC | |
|
Re: Suggestions on how to make array of hashes with this...
by Zaxo (Archbishop) on Sep 21, 2001 at 03:14 UTC | |
by snafu (Chaplain) on Sep 21, 2001 at 09:43 UTC | |
|
Re: Suggestions on how to make array of hashes with this...
by snafu (Chaplain) on Sep 22, 2001 at 00:08 UTC |