Re: This is weird: Pop works but shift doesn't



Harold Castro wrote:
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
.



Relevant Pages

  • Re: Joined Row Update
    ... If you can't wait that long, an alternative is a Foreach loop in a ... -- lock table in exclusive mode is done so as not to run out of locks ... create temp table real_table ... Update table1 ...
    (comp.databases.informix)
  • Accessing Hash keys alphabetically
    ... foreach $temp ) ... sort of the keys. ... I am sure there is some perlish way of doing this with a ...
    (comp.lang.perl.misc)
  • Re: One of thos duh moments
    ... know there is probably some perlish way of doing it better, ... habits die hard) to go through a list of data and build a string that ... foreach $temp ...
    (comp.lang.perl.misc)
  • Re: One of thos duh moments
    ... foreach $temp {#looping through a data set ...
    (comp.lang.perl.misc)
  • err 566/ ISAM 116 - any clues?
    ... There have been no OS errors reported, temp space was very little used ... Check the accompanying ISAM error code for more information. ... The ISAM processor needed to allocate memory for data storage but was ... the entry to the FOREACH. ...
    (comp.databases.informix)