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

2005-04-05 Thread N. Ganesh Babu
Dear All,
I have to convert these lines into individual part of name information 
with a single regular expression.

Input:
B. E. Conway,
J. O. M. Bockris
B. Conway
Output:
B.E.Conway
J.O. M. 
Bockris
B. Conway

Can anybody help me in getting the single regular expression.
Thanks for the help.
Regards,
Ganesh

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



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

2005-04-05 Thread John W. Krahn
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 (){
   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 (  ) {
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]
 



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

2005-04-04 Thread Thomas Bätzler
Harold Castro <[EMAIL PROTECTED]> wrote:
>  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.

Why not do it the simple way:

while(  ){
  s/^(\d+)/scalar localtime( $1 )/e;
  print;
}

or even simpler on the command line:

perl -pne 's/^(\d+)/scalar localtime( $1 )/e'

HTH,
Thomas

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




This is weird: Pop works but shift doesn't

2005-04-04 Thread Harold Castro
Hi,
 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;
open FILE, "./logfile.txt" or die $!;
while (){
   foreach my $time(split/\s+/, $_){
 push @temp, $time;
   }
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.

But if I were to change shift into pop:

our @temp;
open FILE, "./logfile.txt" or die $!;
while (){
   foreach my $time(split/\s+/, $_){
 push @temp, $time;
   }
print pop @temp, "\n";
}


The output will be:

181264589
59460
59045
59045
59661

It seems to be working fine with pop. 

Any idea?

Thank you very much.
















__ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]