[31434] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 2686 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 21 16:09:46 2009

Date: Sat, 21 Nov 2009 13:09:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 21 Nov 2009     Volume: 11 Number: 2686

Today's topics:
        Help with HoH and accessing right keys <cyrusgreats@gmail.com>
    Re: Help with HoH and accessing right keys <ben@morrow.me.uk>
    Re: Help with HoH and accessing right keys <cyrusgreats@gmail.com>
    Re: Help with HoH and accessing right keys <hjp-usenet2@hjp.at>
    Re: Help with HoH and accessing right keys <cyrusgreats@gmail.com>
    Re: Help with HoH and accessing right keys <ben@morrow.me.uk>
    Re: Help with HoH and accessing right keys <cyrusgreats@gmail.com>
    Re: help with regex <cyrusgreats@gmail.com>
    Re: help with regex <martien.verbruggen@invalid.see.sig>
    Re: help with regex <cyrusgreats@gmail.com>
    Re: help with regex <tadmc@seesig.invalid>
    Re: help with regex <jurgenex@hotmail.com>
    Re: help with regex <cyrusgreats@gmail.com>
    Re: help with regex <martien.verbruggen@invalid.see.sig>
    Re: help with regex <someone@example.com>
    Re: help with regex <martien.verbruggen@invalid.see.sig>
    Re: need to do multiple SSH hops using Perl script. <m@rtij.nl.invlalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 21 Nov 2009 08:24:00 -0800 (PST)
From: Obama <cyrusgreats@gmail.com>
Subject: Help with HoH and accessing right keys
Message-Id: <59709aaf-0d03-4827-a825-9bd2efaf1e5b@z35g2000prh.googlegroups.com>

I post before but I was not doing it the right way, here is the
complete code that I need your help, please do let me know if  you do
have any other question. Thanks

#!/usr/bin/perl -w

use Data::Dumper;
my %HoH=();

while (<DATA>)
 {
   if ( /(\S+) (\S+\s\S+\s\S+) (.*?) (\w+) \S+ (\S+) (\S+)( \(([^)]+
\)))?/ )
   {
     if ($6 eq 'Start')
     {
       %HoH = (
          'start_server' => {'start_name' =>$5, 'start_date' => $2,
'start_time' => $3},
          );
     }

     if ($6 eq 'End')
     {
       %HoH = (
          'ende_name' => {'end_name' =>$5, 'end_date' => $2,
'end_time' => $3, 'size' => $7}
          );
     }
  }

}

print Dumper(\%HoH);

=item
What I want is to match right server for right date, time and size,
the out put I'm looking for is:

Server: Hercules:sm_fv_servicedata
  Start date: Sun Nov 15
  Start time: 00:00:03
  End date:   Sun Nov 15
  End time:   00:00:55
  End size:   (53664 KB)

Same for other servers
=cut

__DATA__
dst Sun Nov 15 00:00:03 EST
galaxy.fuqua.duke.edu:fv_servicedataHercules:sm_fv_servicedata Start
dst Sun Nov 15 00:00:03 EST
galaxy.fuqua.duke.edu:fv_phdHercules:sm_fv_phd Start
dst Sun Nov 15 00:00:03 EST
galaxy.fuqua.duke.edu:fv_studentsHercules:sm_fv_students Start
dst Sun Nov 15 00:00:25 EST
galaxy.fuqua.duke.edu:fv_studentsHercules:sm_fv_students End (3368 KB)
dst Sun Nov 15 00:00:26 EST
galaxy.fuqua.duke.edu:fv_phdHercules:sm_fv_phd End (4528 KB)
dst Sun Nov 15 00:00:55 EST
galaxy.fuqua.duke.edu:fv_servicedataHercules:sm_fv_servicedata End
(53664 KB)
dst Sun Nov 15 00:01:00 EST
andromeda.fuqua.duke.edu:esx_fc_nfs2Hercules:sm_esx_fc_nfs2 Request
(Retry)
dst Sun Nov 15 00:15:04 EST
galaxy.fuqua.duke.edu:fv_facultyHercules:sm_fv_faculty Start
dst Sun Nov 15 00:15:04 EST
galaxy.fuqua.duke.edu:fv_researchdataHercules:sm_fv_researchdata Start
dst Sun Nov 15 00:15:04 EST
galaxy.fuqua.duke.edu:fv_embaHercules:sm_fv_emba Start
dst Sun Nov 15 00:15:04 EST
galaxy.fuqua.duke.edu:rootHercules:sm_galaxy_root Start
dst Sun Nov 15 00:15:13 EST
galaxy.fuqua.duke.edu:fv_embaHercules:sm_fv_emba End (1900 KB)
dst Sun Nov 15 00:15:18 EST
galaxy.fuqua.duke.edu:fv_researchdataHercules:sm_fv_researchdata End
(1820 KB)
dst Sun Nov 15 00:15:32 EST
galaxy.fuqua.duke.edu:rootHercules:sm_galaxy_root End (39128 KB)
dst Sun Nov 15 00:16:00 EST
andromeda.fuqua.duke.edu:esx_fc_nfs2Hercules:sm_esx_fc_nfs2 Request
(Retry)


------------------------------

Date: Sat, 21 Nov 2009 17:13:37 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Help with HoH and accessing right keys
Message-Id: <1njlt6-es2.ln1@osiris.mauzo.dyndns.org>


Quoth Obama <cyrusgreats@gmail.com>:
> I post before but I was not doing it the right way, here is the
> complete code that I need your help, please do let me know if  you do
> have any other question. Thanks
> 
> #!/usr/bin/perl -w

You need

    use warnings;
    use strict;

here, and you don't need -w (use warnings is better).

> use Data::Dumper;
> my %HoH=();

Use sensible names for your variables. I know the documentation in
perllol uses names like @AoA: that's because it's just an example, and
the only interesting thing about the variables is that they hold an
array-of-arrays. That isn't the case here (you're more interested in
what the data is than in how it's stored) so you should call it
something like %servers.

> while (<DATA>)
>  {
>    if ( /(\S+) (\S+\s\S+\s\S+) (.*?) (\w+) \S+ (\S+) (\S+)( \(([^)]+
> \)))?/ )

You don't use $1 or $4; also relying on numbered captures makes your
code hard to read. Since your data actually looks like

    dst Sun Nov 15 00:00:25 EST
    galaxy.fuqua.duke.edu:fv_studentsHercules:sm_fv_students End (3368 KB)

(presumably all on one line: you need to be careful to be more clear
about that) you probably want something more like

    if (
        my ($date, $name, $action, $size) =
        /dst (.{10}) (.{8}) \w+ [\w.:]+(Hercules:\w+) (Start|End) (\(.*?\))?/
    ) {

I am here assuming that the date and time are of fixed length, and that
you can safely ignore the timezone: you need to be sure this is correct.
It might be better to extract the whole date-and-time string (including
the timezone) and then parse it with Date::Parse or DateTime or
something. I presume there is a good reason for omitting the year from
the logfile: what's going to happen in a month and a half when you have
some entries from 2009 and some from 2010?

I am also assuming that the part of the 'name' string you want always
starts with 'Hercules:'. If this isn't correct you will need to work out
what the correct rules are.

>    {
>      if ($6 eq 'Start')

    if ($action eq "Start") {

>      {
>        %HoH = (
>           'start_server' => {'start_name' =>$5, 'start_date' => $2,
> 'start_time' => $3},
>           );

This will set %HoH to just one entry, deleting any other entries you've
already parsed. Since you are trying to build up a set of records about
each server, you need to use the server name as a key in the hash. Since
you want two entries for each server (a 'start' and an 'end' record) you
need a further key below that:

    $servers{$name}{start_server} = {
        date    => $date,
        time    => $time,
    };

Notice that I didn't need to worry about creating $servers{$name} or
putting a new hashref in it: I just assumed it exists and contained one.
Perl allows you to do this, for convenience.

You need a similar assignment in the 'End' block; and, in fact, since
these two assignments are pretty-much the same, you could leave off the
'if' blocks altogether and just do

    $servers{$name}{$action} = {
        date    => $date,
        time    => $time,
        size    => $size,
    };

This will mean your 'Start' records end up with a 'size' key set to
undef, but that doesn't matter. (Notice that the fact I changed the
regex above to ignore the lines that weren't 'Start' or 'End' makes this
section of the code simpler.)

> print Dumper(\%HoH);

Obviously at this point you will need to print the data out in the
correct format, rather than simply using Dumper. You want to leave the
Dumper in for now, though, so you can see what the data structure looks
like. Your printing code will need to start with

    for my $server (keys %servers) {

Ben



------------------------------

Date: Sat, 21 Nov 2009 11:09:06 -0800 (PST)
From: Obama <cyrusgreats@gmail.com>
Subject: Re: Help with HoH and accessing right keys
Message-Id: <9633a289-ba4a-4151-8352-3a7bbe4b2e22@u18g2000pro.googlegroups.com>

> (presumably all on one line: you need to be careful to be more clear
> about that) you probably want something more like
>
> =A0 =A0 if (
> =A0 =A0 =A0 =A0 my ($date, $name, $action, $size) =3D
> =A0 =A0 =A0 =A0 /dst (.{10}) (.{8}) \w+ [\w.:]+(Hercules:\w+) (Start|End)=
 (\(.*?\))?/
> =A0 =A0 ) {
>

Correct, all is one line such as below:
dst Sun Nov 15 00:00:03 EST galaxy.fuqua.duke.edu:fv_phd
Hercules:sm_fv_phd Start

>
> I am also assuming that the part of the 'name' string you want always
> starts with 'Hercules:'. If this isn't correct you will need to work out
> what the correct rules are.

No 'name' does not always starts with 'Hercules:' it could be
different as each log is different.
also ^dst is not the case always , it could be slk or src

Per your recommendation I created the following, but it does not work:

#!/usr/bin/perl -w
use warnings;
use strict;

use Data::Dumper;
my %servers;

my $time;
while (<DATA>) {
   if (my ($date, $name, $action, $size) =3D /dst (.{10}) (.{8}) \w+
[\w.:]+(Hercules:\w+) (Start|End) (\(.*?\))?/ ) {
    print "[DBG]: $date, $name, $action, $size\n";
        if ($action eq "Start") {
            $servers{$name}{start_server} =3D {
            'date'    =3D> $date,
            'time'    =3D> $time, <-- thi is not defined and it
complains
           };
        }
        $servers{$name}{$action} =3D {
            'date '   =3D> $date,
            'time'    =3D> $time, <--- this is not defined complains
            'size '   =3D> $size,
        };
   }
}

#print Dumper(\%$servers);

 for my $server (keys %servers) {
   my $start_server=3D  $servers->{ $server }{ start_server };
   my $start_date  =3D  $servers->{ $server }{ start_server }->{ Start }-
>{'date' };
   my $start_time  =3D  $servers->{ $server }{ start_server }->{ Start }-
>{ 'time' };
   my $end_date    =3D  $servers->{ $server }{ start_server }->{ End }->
{ 'date' };
   my $end_time    =3D  $servers->{ $server }{ start_server }->{ End }->
{ 'time' };
   my $end_size    =3D  $servers->{ $server }{ start_server }->{ End }->
{ 'size' };

   #print "Start server :$start_server\nStart Date: $start_date
$start_time\nEnd Date: $end_date $end_time\nSize: $end_size\n\n";
 }


------------------------------

Date: Sat, 21 Nov 2009 20:31:50 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Help with HoH and accessing right keys
Message-Id: <slrnhggg18.loc.hjp-usenet2@hrunkner.hjp.at>

On 2009-11-21 17:13, Ben Morrow <ben@morrow.me.uk> wrote:
> I presume there is a good reason for omitting the year from the
> logfile:

The "good reason" is probably that the author of the log generating
software was influenced by syslog which doesn't include the year either
(which can be a major pain in the ass when you have to process old
logfiles).

	hp


------------------------------

Date: Sat, 21 Nov 2009 11:42:39 -0800 (PST)
From: Obama <cyrusgreats@gmail.com>
Subject: Re: Help with HoH and accessing right keys
Message-Id: <7e296652-8281-4c78-ae8d-0d61f856f8ea@z4g2000prh.googlegroups.com>

I think the regex is not correct since my has-of-has i empty means it
does not match the regex!

^dst (.{10}) (.{8}) \w+ [\w.:]+(Hercules:\w+) (Start|End) (\(.*?\))


------------------------------

Date: Sat, 21 Nov 2009 20:01:05 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Help with HoH and accessing right keys
Message-Id: <1htlt6-irn.ln1@osiris.mauzo.dyndns.org>


Quoth Obama <cyrusgreats@gmail.com>:
> > (presumably all on one line: you need to be careful to be more clear
> > about that) you probably want something more like
> >
> >     if (
> >         my ($date, $name, $action, $size) =
> >         /dst (.{10}) (.{8}) \w+ [\w.:]+(Hercules:\w+) (Start|End) (\(.*?\))?/

(I made an error here: I meant 

    my ($date, $time, $name, $action, $size)

)

> >     ) {
> >
> 
> Correct, all is one line such as below:
> dst Sun Nov 15 00:00:03 EST galaxy.fuqua.duke.edu:fv_phd
> Hercules:sm_fv_phd Start
> 
> >
> > I am also assuming that the part of the 'name' string you want always
> > starts with 'Hercules:'. If this isn't correct you will need to work out
> > what the correct rules are.
> 
> No 'name' does not always starts with 'Hercules:' it could be
> different as each log is different.

Then how do you tell where the section of the name you are interested in
starts? Your example line above suggests the two pieces are
space-separated, which your original example did not.

> also ^dst is not the case always , it could be slk or src

So, modify the regex accordingly.

> Per your recommendation I created the following, but it does not work:

'Does not work' is not a useful problem description.

> #!/usr/bin/perl -w
> use warnings;
> use strict;
> 
> use Data::Dumper;
> my %servers;
> 
> my $time;
> while (<DATA>) {
>    if (my ($date, $name, $action, $size) = /dst (.{10}) (.{8}) \w+
> [\w.:]+(Hercules:\w+) (Start|End) (\(.*?\))?/ ) {
>     print "[DBG]: $date, $name, $action, $size\n";
>         if ($action eq "Start") {
>             $servers{$name}{start_server} = {
>             'date'    => $date,
>             'time'    => $time, <-- thi is not defined and it
> complains

Yes, that was the error I pointed out above. You could have worked that
out for yourself.

>            };
>         }
>         $servers{$name}{$action} = {
>             'date '   => $date,
>             'time'    => $time, <--- this is not defined complains
>             'size '   => $size,
>         };

Oh, for goodness' sake! Did you actually *read* what I wrote, the words
as well as the code? This assignment (to ...{$action}) is *instead* of
the if blocks, not as well as.

>    }
> }
> 
> #print Dumper(\%$servers);
> 
>  for my $server (keys %servers) {
>    my $start_server=  $servers->{ $server }{ start_server };

You don't have a $servers variable. You do have a %servers variable.

>    my $start_date  =  $servers->{ $server }{ start_server }->{ Start }-
> >{'date' };
>    my $start_time  =  $servers->{ $server }{ start_server }->{ Start }-
> >{ 'time' };
>    my $end_date    =  $servers->{ $server }{ start_server }->{ End }->
> { 'date' };
>    my $end_time    =  $servers->{ $server }{ start_server }->{ End }->
> { 'time' };
>    my $end_size    =  $servers->{ $server }{ start_server }->{ End }->
> { 'size' };

No, you have completely misunderstood how this data structure is
supposed to work (and adding the 'Start' records twice, under different
names, doesn't help). Print a Dumper of the structure and then study it
until you understand what ends up where.

As a point of style, repeating essentially the same deref expression
over and over is terribly messy. Assign it to a temporary:

    my $start = $servers{$server}{Start};
    my $end   = $servers{$server}{End};

    print <<OUT;
    Server: $server
        Start Date: $start->{date}
        ...
    OUT

Ben



------------------------------

Date: Sat, 21 Nov 2009 12:55:14 -0800 (PST)
From: Obama <cyrusgreats@gmail.com>
Subject: Re: Help with HoH and accessing right keys
Message-Id: <4d4f0a97-da69-401a-af6f-dfc73e94a682@g1g2000pra.googlegroups.com>

As a point of style, repeating essentially the same deref expression
> over and over is terribly messy. Assign it to a temporary:
>
> =A0 =A0 my $start =3D $servers{$server}{Start};
> =A0 =A0 my $end =A0 =3D $servers{$server}{End};
>
> =A0 =A0 print <<OUT;
> =A0 =A0 Server: $server
> =A0 =A0 =A0 =A0 Start Date: $start->{date}
> =A0 =A0 =A0 =A0 ...
> =A0 =A0 OUT
>
> Ben

Thank you so much Ben, I got it to work god bless you


------------------------------

Date: Fri, 20 Nov 2009 21:01:01 -0800 (PST)
From: Obama <cyrusgreats@gmail.com>
Subject: Re: help with regex
Message-Id: <29b3c737-aab3-4404-a5d8-f367563429d6@u8g2000prd.googlegroups.com>

On Nov 20, 5:05=A0pm, Obama <cyrusgre...@gmail.com> wrote:
> Thanks Jim,
> then how do you access it each one?

Will someone please help me on this, thanks in advance..
Jim, it does not work
my %times;
while(<FILE>) {
  if( /(\S+) (\S+\s\S+\s\S+) (.*?) (\w+) \S+ (\S+) (\S+)( \(([^)]+
\)))?/) {
    $times{$5}->{$6} =3D $3;
    if( $6 eq 'Start' ) {
      $times{$5}->{Date} =3D $2;  <----should this be 'Date' not Date
    }elsif( $6 eq 'End' ) {
      $times{$5}->{Size} =3D $7;  <----- same for this one
    }
  }

}

Also how to you access teach key such as server, date/time, for both
start and end and size?



------------------------------

Date: Sat, 21 Nov 2009 16:46:59 +1100
From: Martien Verbruggen <martien.verbruggen@invalid.see.sig>
Subject: Re: help with regex
Message-Id: <jsu7eh.o6g.ln@news.heliotrope.home>

On Fri, 20 Nov 2009 21:01:01 -0800 (PST),
	Obama <cyrusgreats@gmail.com> wrote:
> On Nov 20, 5:05 pm, Obama <cyrusgre...@gmail.com> wrote:
>> Thanks Jim,
>> then how do you access it each one?
>
> Will someone please help me on this, thanks in advance..
> Jim, it does not work

Please, read this:

http://www.rehabitation.com/clpmisc.shtml

You're not making it easy for anyone to help you. Make it easier. Read
that link. Do what it suggests.

When I tried to look at your problem, I had to make chanes to the both
the code, and the data. Even after making the snippet that you provided
syntactically correct, it still wouldn't run with strict, and only with
a large number of warnings without. 

So I gave up.

If you had been a bit more pro-activem there would have been a decent
chance you would have had a solution by now.

Martien
-- 
                             | 
Martien Verbruggen           | In the fight between you and the world,
first.last@heliotrope.com.au | back the world - Franz Kafka
                             | 


------------------------------

Date: Fri, 20 Nov 2009 23:00:11 -0800 (PST)
From: Obama <cyrusgreats@gmail.com>
Subject: Re: help with regex
Message-Id: <f59fe249-ff80-49c1-aa0c-947ced859a6d@o9g2000prg.googlegroups.com>

>
> So I gave up.
>
Please don't, I'm new to this and please be patient I try my best,
here what I got so far

open (FILE, $file) || die "Can't open datafile '$file' $!";

while (<FILE>)
 {
   next if (/^\s*$/);  # skip empty lines
   next if (/^slk/);  # skip empty lines
   next if (/^=/);  # skip = lines

   if ( /(\S+) (\S+\s\S+\s\S+) (.*?) (\w+) \S+ (\S+) (\S+)( \(([^)]+
\)))?/ )
   {
     if ($6 eq 'Start')
     {
       $end{$5}= {
          s_name => $5,
          s_date => $2,
          s_time => $3,
        };
     }
     if ($6 eq 'End')
     {
       $end{$5}= {
          e_name => $5,
          e_date => $2,
          e_time => $3,
          e_size => $7
        };
     }
  }
}
 close (FILE);

print Dumper(%end);

I got the following

$VAR1 = 'Hercules:sm_fv_mmvideo';
$VAR2 = {
          'e_name' => 'Hercules:sm_fv_mmvideo',
          'e_size' => ' (692 KB)',
          'e_time' => '15:30:17',
          'e_date' => 'Wed Nov 18'
        };
$VAR3 = 'Hercules:sm_esx_sata_nfs1';
$VAR4 = {
          'e_name' => 'Hercules:sm_esx_sata_nfs1',
          'e_size' => ' (1161316 KB)',
          'e_time' => '15:42:12',
          'e_date' => 'Wed Nov 18'
        };
what is missing the:
s_name => $5,
          s_date => $2,
          s_time => $3,


Basically I want to make sure the server has right info (start and end
time) since there are other server as listed above from log....


foreach $key (keys %end){       # loop thru the hash
  my $s_server = $end{$key}{s_name};   # retrieve the name
  my $e_server = $end{$key}{e_name};   # retrieve the name

  my $s_date = $end{$key}{s_date};   # retrieve date
  my $e_date = $end{$key}{e_date};   # retrieve date

  my $s_time = $end{$key}{s_time};   # retrieve time
  my $e_time = $end{$key}{e_time};   # retrieve time


  my $e_size = $end{$key}->{e_size};  # size

  print "$key:\n------------------\nStart: $s_server\nDate: $s_date
\nTime: $s_time\n";;
  print "$key:\n------------------\nEnd: $e_server\nDate: $e_date
\nTime: $e_time\nSize: $e_size\n\n";;
}



Thanks again and hope I can get something from you good guys...

the log file has


------------------------------

Date: Sat, 21 Nov 2009 07:22:29 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: help with regex
Message-Id: <slrnhgfq74.270.tadmc@tadbox.sbcglobal.net>

Obama <cyrusgreats@gmail.com> wrote:
>>
>> So I gave up.
>>
> Please don't, 


Did you read all of the paragraphs before that part?

Did you understand what they were saying?

Are you trying to correct the deficiencies pointed out there?

Did you read the Posting Guidelines?


> I'm new to this 


New to Perl is no problem.

New to programming is no problem.

Most folks will not ignore you for that.

New to asking a coherent question on Usenet newsgroups can become
a problem, so concentrate on getting that right before it is too late.


> and please be patient I try my best,


I am sorry, but I do not believe you. 

I do not think you are trying very hard at all.

The Posting Guidelines suggest you post a *complete* program that
we can run. (my earlier followup to you included a complete program
for example.)

You did not do that.

The Posting Guidelines suggest that you include file data in
a __DATA__ section (also as has been shown to you in my earlier
followup).

You did not do that either.


>    next if (/^slk/);  # skip empty lines


That code does not "skip empty lines"...


> print Dumper(%end);


You should pass a *reference* to Dumper:

    print Dumper(\%end);


> hope I can get something from you good guys...


You are still making it hard for us to do that.

If you make it easy for us to help you, then it is more likely
that we will end up actually helping you...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Sat, 21 Nov 2009 06:20:27 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: help with regex
Message-Id: <pbtfg59ocrfrm8nmqg4lb0l9m6mfkshobj@4ax.com>

Obama <cyrusgreats@gmail.com> wrote:
>On Nov 20, 5:05 pm, Obama <cyrusgre...@gmail.com> wrote:
>> Thanks Jim,
>> then how do you access it each one?
>
>Will someone please help me on this, thanks in advance..
>Jim, it does not work

Who is Jim, what did he do, and what doesn't work?

>my %times;
>while(<FILE>) {
>  if( /(\S+) (\S+\s\S+\s\S+) (.*?) (\w+) \S+ (\S+) (\S+)( \(([^)]+
>\)))?/) {
>    $times{$5}->{$6} = $3;
>    if( $6 eq 'Start' ) {
>      $times{$5}->{Date} = $2;  <----should this be 'Date' not Date

No, not necessarily. Using barewords as keys for hashes is perfectly
fine as long as they don't contain whitespace.

>    }elsif( $6 eq 'End' ) {
>      $times{$5}->{Size} = $7;  <----- same for this one
>    }
>  }
>
>}
>
>Also how to you access teach key such as server, date/time, for both
>start and end and size?

There are no such keys in the code sample you showed above. The only
keys used are 'End', 'Size', and whatever the content of $5 and $6 are.

Either way, you got a Hash-of-Hashes, so the answer would depend upon if
you are talking about keys for the top-level hash or the sub-hashes.

jue


------------------------------

Date: Sat, 21 Nov 2009 08:19:05 -0800 (PST)
From: Obama <cyrusgreats@gmail.com>
Subject: Re: help with regex
Message-Id: <7432535a-d94d-4101-99df-4e5e9bfd5a12@f20g2000prn.googlegroups.com>

Here is complete code that I need your help, please do let me know if
you do have any other question.

#!/usr/bin/perl -w

use Data::Dumper;
my %HoH=();

while (<DATA>)
 {
   if ( /(\S+) (\S+\s\S+\s\S+) (.*?) (\w+) \S+ (\S+) (\S+)( \(([^)]+
\)))?/ )
   {
     if ($6 eq 'Start')
     {
       %HoH = (
          'start_server' => {'start_name' =>$5, 'start_date' => $2,
'start_time' => $3},
          );
     }

     if ($6 eq 'End')
     {
       %HoH = (
          'ende_name' => {'end_name' =>$5, 'end_date' => $2,
'end_time' => $3, 'size' => $7}
          );
     }
  }
}

print Dumper(\%HoH);

=item
What I want is to match right server for right date, time and size,
the out put I'm looking for is:

Server: Hercules:sm_fv_servicedata
  Start date: Sun Nov 15
  Start time: 00:00:03
  End date:   Sun Nov 15
  End time:   00:00:55
  End size:   (53664 KB)

Same for other servers
=cut

__DATA__
dst Sun Nov 15 00:00:03 EST galaxy.fuqua.duke.edu:fv_servicedata
Hercules:sm_fv_servicedata Start
dst Sun Nov 15 00:00:03 EST galaxy.fuqua.duke.edu:fv_phd
Hercules:sm_fv_phd Start
dst Sun Nov 15 00:00:03 EST galaxy.fuqua.duke.edu:fv_students
Hercules:sm_fv_students Start
dst Sun Nov 15 00:00:25 EST galaxy.fuqua.duke.edu:fv_students
Hercules:sm_fv_students End (3368 KB)
dst Sun Nov 15 00:00:26 EST galaxy.fuqua.duke.edu:fv_phd
Hercules:sm_fv_phd End (4528 KB)
dst Sun Nov 15 00:00:55 EST galaxy.fuqua.duke.edu:fv_servicedata
Hercules:sm_fv_servicedata End (53664 KB)
dst Sun Nov 15 00:01:00 EST andromeda.fuqua.duke.edu:esx_fc_nfs2
Hercules:sm_esx_fc_nfs2 Request (Retry)
dst Sun Nov 15 00:15:04 EST galaxy.fuqua.duke.edu:fv_faculty
Hercules:sm_fv_faculty Start
dst Sun Nov 15 00:15:04 EST galaxy.fuqua.duke.edu:fv_researchdata
Hercules:sm_fv_researchdata Start
dst Sun Nov 15 00:15:04 EST galaxy.fuqua.duke.edu:fv_emba
Hercules:sm_fv_emba Start
dst Sun Nov 15 00:15:04 EST galaxy.fuqua.duke.edu:root
Hercules:sm_galaxy_root Start
dst Sun Nov 15 00:15:13 EST galaxy.fuqua.duke.edu:fv_emba
Hercules:sm_fv_emba End (1900 KB)
dst Sun Nov 15 00:15:18 EST galaxy.fuqua.duke.edu:fv_researchdata
Hercules:sm_fv_researchdata End (1820 KB)
dst Sun Nov 15 00:15:32 EST galaxy.fuqua.duke.edu:root
Hercules:sm_galaxy_root End (39128 KB)
dst Sun Nov 15 00:16:00 EST andromeda.fuqua.duke.edu:esx_fc_nfs2
Hercules:sm_esx_fc_nfs2 Request (Retry)





------------------------------

Date: Sun, 22 Nov 2009 07:22:33 +1100
From: Martien Verbruggen <martien.verbruggen@invalid.see.sig>
Subject: Re: help with regex
Message-Id: <96i9eh.415.ln@news.heliotrope.home>

On Fri, 20 Nov 2009 23:00:11 -0800 (PST),
	Obama <cyrusgreats@gmail.com> wrote:
>>
>> So I gave up.
>>
> Please don't, I'm new to this and please be patient I try my best,
> here what I got so far

And this is what I see when I copy and paste (which is ALL I should have
to do), and add

#!/usr/bin/perl
use warnings;
use strict;

Which is what YOU should have already done.

Global symbol "$file" requires explicit package name at /tmp/foo.pl line 5.
Global symbol "$file" requires explicit package name at /tmp/foo.pl line 5.
Global symbol "%end" requires explicit package name at /tmp/foo.pl line 18.
Global symbol "%end" requires explicit package name at /tmp/foo.pl line 26.
Global symbol "%end" requires explicit package name at /tmp/foo.pl line 37.
Execution of /tmp/foo.pl aborted due to compilation errors.

Again, I gave up.

Did you follow that link that I suggested? And did you read all it said
and all it referred to? If so, I suggest you read it again.

You're simply making it too hard for anyone to help you.

Martien
-- 
                             | 
Martien Verbruggen           | In the fight between you and the world,
first.last@heliotrope.com.au | back the world - Franz Kafka
                             | 


------------------------------

Date: Sat, 21 Nov 2009 12:34:04 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: help with regex
Message-Id: <69YNm.6361$kY2.4171@newsfe01.iad>

Jürgen Exner wrote:
> Obama <cyrusgreats@gmail.com> wrote:
>> On Nov 20, 5:05 pm, Obama <cyrusgre...@gmail.com> wrote:
>>> Thanks Jim,
>>> then how do you access it each one?
>> Will someone please help me on this, thanks in advance..
>> Jim, it does not work
> 
> Who is Jim, what did he do, and what doesn't work?
> 
>> my %times;
>> while(<FILE>) {
>>  if( /(\S+) (\S+\s\S+\s\S+) (.*?) (\w+) \S+ (\S+) (\S+)( \(([^)]+
>> \)))?/) {
>>    $times{$5}->{$6} = $3;
>>    if( $6 eq 'Start' ) {
>>      $times{$5}->{Date} = $2;  <----should this be 'Date' not Date
> 
> No, not necessarily. Using barewords as keys for hashes is perfectly
> fine as long as they don't contain whitespace.

as long as they don't contain any \W characters.



John
-- 
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway


------------------------------

Date: Sun, 22 Nov 2009 08:07:52 +1100
From: Martien Verbruggen <martien.verbruggen@invalid.see.sig>
Subject: Re: help with regex
Message-Id: <8rk9eh.lo6.ln@news.heliotrope.home>

On Sat, 21 Nov 2009 08:19:05 -0800 (PST),
	Obama <cyrusgreats@gmail.com> wrote:
> Here is complete code that I need your help, please do let me know if
> you do have any other question.

Thanks for submitting code that compiled. Using that as a starting
point, I came up with a possible solution to your problem. I had to fix
your regex to correctly capture the size information in $7, but other
than that, it seems to work. 

You should try to convince your posting software to not wrap long lines.
I had to reassemble the log lines. Not a huge deal.



In the below, I'm using the %data hash to store information about each
'server'. Under each server key is a hash reference with keys 'start'
and 'end'. Each start hash has two keys, and each end one three. Other
data structures can be used, particularly for the last level, but I
figured this was closest to what you were trying to do, and therefore
probably most comfortable. I noticed you know about Data::Dumper, so use 

print Dumper(\%data);

somewhere near the end of the program to see what the structure looks
like if you want.

Depending on requirements I wasn't aware of when I was writing this, you
may need to change one or two things. This should give you a decent
start though.

If you have any questions, don't hesistate to ask.

Martien

PS. Here's the program:


#!/usr/bin/perl
use strict;
use warnings;
my %data;

while (<DATA>)
{
    if (/(\S+) (\S+\s\S+\s\S+) (.*?) (\w+) \S+ (\S+) (\S+)(?: \(([^)]+)\))?/) 
    {
	if ($6 eq 'Start') 
	{
	    $data{$5}{start} = {date => $2, time => $3};
	}
	elsif ($6 eq 'End') 
	{
	    $data{$5}{end} = {date => $2, time => $3, size => $7};
	}
    }
}

sub print_start 
{
    my ($start) = @_;
    if ($start) 
    {
	printf "  Start date: %s\n  Start time: %s\n",
	    $start->{date}, $start->{time};
    } 
    else 
    {
	print "  No start information\n";
    }
}

sub print_end 
{
    my ($end) = @_;
    if ($end) 
    {
	printf "  End date: %s\n  End time: %s\n  End size: %s\n",
	    $end->{date}, $end->{time}, $end->{size};
    } 
    else 
    {
	print "  No end information\n";
    }
}

while (my ($name, $data) = each %data)
{
    print "Server: $name\n";
    print_start($data->{start});
    print_end($data->{end});
}

__DATA__
dst Sun Nov 15 00:00:03 EST galaxy.fuqua.duke.edu:fv_servicedata Hercules:sm_fv_servicedata Start
dst Sun Nov 15 00:00:03 EST galaxy.fuqua.duke.edu:fv_phd Hercules:sm_fv_phd Start
dst Sun Nov 15 00:00:03 EST galaxy.fuqua.duke.edu:fv_students Hercules:sm_fv_students Start
dst Sun Nov 15 00:00:25 EST galaxy.fuqua.duke.edu:fv_students Hercules:sm_fv_students End (3368 KB)
dst Sun Nov 15 00:00:26 EST galaxy.fuqua.duke.edu:fv_phd Hercules:sm_fv_phd End (4528 KB)
dst Sun Nov 15 00:00:55 EST galaxy.fuqua.duke.edu:fv_servicedata Hercules:sm_fv_servicedata End (53664 KB)
dst Sun Nov 15 00:01:00 EST andromeda.fuqua.duke.edu:esx_fc_nfs2 Hercules:sm_esx_fc_nfs2 Request (Retry)
dst Sun Nov 15 00:15:04 EST galaxy.fuqua.duke.edu:fv_faculty Hercules:sm_fv_faculty Start
dst Sun Nov 15 00:15:04 EST galaxy.fuqua.duke.edu:fv_researchdata Hercules:sm_fv_researchdata Start
dst Sun Nov 15 00:15:04 EST galaxy.fuqua.duke.edu:fv_emba Hercules:sm_fv_emba Start
dst Sun Nov 15 00:15:04 EST galaxy.fuqua.duke.edu:root Hercules:sm_galaxy_root Start
dst Sun Nov 15 00:15:13 EST galaxy.fuqua.duke.edu:fv_emba Hercules:sm_fv_emba End (1900 KB)
dst Sun Nov 15 00:15:18 EST galaxy.fuqua.duke.edu:fv_researchdata Hercules:sm_fv_researchdata End (1820 KB)
dst Sun Nov 15 00:15:32 EST galaxy.fuqua.duke.edu:root Hercules:sm_galaxy_root End (39128 KB)
dst Sun Nov 15 00:16:00 EST andromeda.fuqua.duke.edu:esx_fc_nfs2 Hercules:sm_esx_fc_nfs2 Request (Retry)

-- 
                             | Everything that can be invented has been
Martien Verbruggen           | invented. -- Charles H. Duell,
first.last@heliotrope.com.au | Commissioner, U.S. Office of Patents,
                             | 1899


------------------------------

Date: Sat, 21 Nov 2009 16:21:23 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: need to do multiple SSH hops using Perl script.
Message-Id: <j4dlt6-9le.ln1@news.rtij.nl>

On Fri, 20 Nov 2009 01:58:41 -0800, pradeep nair wrote:

> The script must:
> 
>    1. ssh to system A ,from there ssh to system B & from there ssh to
> system C.
>    2. At system C need to connect to a DB and execute few queries and
> create an output file
>    3. The outfile should be SCpied to Sytem A
> 

If the script is on host C:
ssh A ssh b ssh C <script> >outfile

If the script is on host A:
cat <script> | ssh A ssh b ssh C sh -  >outfile

So first make a script on C that works, then try to execute it via ssh

HTH,
M4



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 2686
***************************************


home help back first fref pref prev next nref lref last post