Hi,
Hello,
I'm parsing a log file that contains this format:
1112677214 31388202 181264589 1112677214 8843 59460 8843 59460 1112676919 10728 59045 10728 59045 1112676900 10617 59006 10728 59045 1112676600 8693 58389 9531 59661
These logs are in unix timestamp format: I'm trying to convert the first column into scalar localtime.
Here's my code:
our @temp;
Why use our(), do you really need a package variable?
open FILE, "./logfile.txt" or die $!; while (<FILE>){ foreach my $time(split/\s+/, $_){ push @temp, $time; }
No need for a loop there:
push @temp, split;
print shift @temp, "\n"; }
... And the output:
1112677214 31388202 181264589 1112677214 8843 59460 8843 59460 1112676919 10728 59045 10728 59045 1112676900 10617 59006 10728 59045 1112676600 8693 58389 9531 59661
This is wiered, it didn't print the very first element of @temp.
Probably because you are push()ing everything onto @temp so that the array grows for each iteration of the loop. When you read the first line of the file you push (1112677214, 31388202, 181264589) onto @temp and then shift 1112677214 off. On the second line you push (1112677214, 8843, 59460, 8843, 59460) onto @temp so it now contains (31388202, 181264589, 1112677214, 8843, 59460, 8843, 59460) and then you shift 31388202 off.
If you only want the first column then use a list slice:
while ( <FILE> ) { my $time = ( split )[ 0 ]; print "$time\n"; }
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>