[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [REDHAT] Re: [offtopic]Perl:assigning an array



On Mon, 21 Feb 2000, David Kramer wrote:

> Robert Canary wrote:
> > 
> > Hello GreyBear,
> > 
> > The problem is the the items(item1,item2,item3) are coming from a log file of
> > another app.  So I must first must split them from the line that I am cutting
> > out of the logfile.  The log file line states them as item1='value1',
> > tem2='value2' all on one line there is seven item=value pairs on one line of
> > extracted text.  I can capture the text of the log file.  But I can't get it
> > into an array.   I was trying the item/value scenario as though you would read
> > it from a file.  But the only way I can get a loop to roll over is if it is an
> > array.
> 
> OH.  I didn't know there would be multiple a=b on the same line.  try
> this:
> --------------------------
> open(IN,"foo") or die("$0: Could not open foo for read: $1");
> 
> #Read file, load array
> while(<IN>)
> {
> 	chomp();
> 	$Line=$_;
> 	while($Line =~ s/^(\w+)=(\w+)\,{0,1}\s*//)
> 	{
> 		if($1 ne '')
> 		{
> 			$Line{$1}=$2;
> 		}
> 	}
> }
> 
> foreach $Key (keys(%Line)) 
> {
> 	print("[$Key]=[$Line{$Key}]\n");
> }

================

open(IN, logfile) ;

while(<IN>) {
	foreach (split) {
		($key, $value) = split("=") ;
		print "$key, $value\n" ;
	}
}
close IN ;

================

  once you get the values in $key and $value, do with them whatever
you wish.

rday




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]