Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wats wrong with this code.

Status
Not open for further replies.

technoman007

IS-IT--Management
Jul 17, 2012
9
US
so here im jst tryin to push all lines with todays date into the array.
but all the data is going thru and getting saved in the array.

$var has this thing
adminode Sun Mar 11 01:53:16 EST 2012 - technoman - sleeping nodename
adminnode Sun Mar 11 01:53:37 EST 2012 - superman - awake nodename
adminnode Sun Jul 19 01:53:37 EST 2012 - superman - awake nodename


so logicaly only the last line should go thru (cos im comparing awk '{print $3}' in date command to awk print $4 in the three lines above).. but all the three lines are goin thru


open(fho,"/home/technoman3/$node.txt");
foreach(<fho>)
{
$var=$_;
#chomp($var);
$i=`date | awk '{print $3}'`;
$p=`echo '$var' | awk '{print $4}'`;
if($p==$i)
{
push(@y,"$var");
}
#push(@y,"$var") if $p == $i;

}
}
$p=0;
foreach(@y)
{
print $_ ;

}
 
Feherke .. thanks a lot .. that was my mistake .. i rem reading that in a book .. but it had slipped from my mind ..
Feherke u suggested that this not a healthy perl practice .. could u please suggest how to do the same in perl .. can this be done without using command line commands.
 
Hi

technoman007 said:
can this be done without using command line commands.
Certainly can.

This is how your loop should look :
Code:
[b]my[/b] [navy]@time[/navy][teal]=[/teal]localtime[teal];[/teal]

[b]foreach[/b] [teal]([/teal][green][i]<fho>[/i][/green][teal])[/teal] [teal]{[/teal]
  [b]my[/b] [navy]@part[/navy][teal]=[/teal][b]split[/b][teal];[/teal]
  [b]push[/b] [navy]@y[/navy][teal],[/teal][navy]$_[/navy] [b]if[/b] [navy]$part[/navy][teal][[/teal][purple]3[/purple][teal]]==[/teal][navy]$time[/navy][teal][[/teal][purple]3[/purple][teal]];[/teal]
[teal]}[/teal]

Regarding the use of command line tools, if you want so, Perl itself is also a command line tool :
Code:
perl -nae 'BEGIN[teal]{[/teal][navy]@t[/navy][teal]=[/teal]localtime[teal]}[/teal][b]print[/b] [b]if[/b][navy]$F[/navy][teal][[/teal][purple]3[/purple][teal]]==[/teal][navy]$t[/navy][teal][[/teal][purple]3[/purple][teal]][/teal]' /home/technoman3/$node.txt


Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top