Q: Analyse data and provide a report - Arrays?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Troll

    #16
    Re: Q: Analyse data and provide a report - Arrays?

    Thanks very much.

    I'm having a bit of drama within my parsing loop.

    If I'm trying to look for a specific pattern [ie. tcp] then I am able to
    find it [by printing a 'found' message]. This message is then printed each
    and every time 'tcp' is found [for a total of 6 times on 6 separate lines].
    The script then finishes.

    But if I'm trying to increment the number of times this pattern was found I
    get the dreaded error:
    Use of uninitialized value in hash element at ...

    Here's the code extract:
    while (<>) {
    my($Proto)=
    /(\s+)*$/;

    if (/tcp/) {
    print 'found';
    $Protos{$Proto} ++;


    where am I failing ?


    Comment

    • Troll

      #17
      Re: Q: Analyse data and provide a report - Arrays?

      OK, I had some luck getting the first value incremented but no more.

      Version which works:
      *************** **
      if (/tcp/) {
      my($Proto)=
      /^(\w+)/;
      $Protos{$Proto} ++;
      }
      print "TCP = $Protos{'tcp'}\ n";

      #output section
      TCP = 6 # all is correct here


      Version which does not work:
      *************** *******
      if (/tcp/) {
      my($Proto, $RecvQ)=
      /^(\w+) (\s+)/;
      $Protos{$Proto} ++;
      $RecvQs{$RecvQ) ++;
      }
      print "TCP = $Protos{'tcp'}\ n";
      print "RecvQ = $RecvQs{'0'}\n" ;

      #output section
      TCP = 6 # all is correct here
      Use of uninitialized value in concatenation (.) or string at... # error
      time - this refers to the 2nd print statement
      RecvQ = # this is blank

      I have tried reading the second parameter as a (\s+) and as a (\d+) with no
      luck. If you run netstat you will probably see that all items in the RecvQ
      column are 0.
      What have I done wrong now?

      Can a number of whitespaces be represented by:
      /^(\w+) (\s+)/; # this is a word followed by some spaces followed by a
      string
      or is the above only ONE whitespace?


      Comment

      • John Bokma

        #18
        Re: Q: Analyse data and provide a report - Arrays?

        Troll wrote:
        [color=blue]
        > OK, I had some luck getting the first value incremented but no more.
        >
        > Version which works:
        > *************** **
        > if (/tcp/) {
        > my($Proto)=
        > /^(\w+)/;
        > $Protos{$Proto} ++;
        > }
        > print "TCP = $Protos{'tcp'}\ n";
        >
        > #output section
        > TCP = 6 # all is correct here
        >
        >
        > Version which does not work:
        > *************** *******
        > if (/tcp/) {
        > my($Proto, $RecvQ)=
        > /^(\w+) (\s+)/;
        > $Protos{$Proto} ++;
        > $RecvQs{$RecvQ) ++;
        > }
        > print "TCP = $Protos{'tcp'}\ n";
        > print "RecvQ = $RecvQs{'0'}\n" ;
        >
        > #output section
        > TCP = 6 # all is correct here
        > Use of uninitialized value in concatenation (.) or string at... # error
        > time - this refers to the 2nd print statement
        > RecvQ = # this is blank
        >
        > I have tried reading the second parameter as a (\s+) and as a (\d+) with no
        > luck. If you run netstat you will probably see that all items in the RecvQ
        > column are 0.
        > What have I done wrong now?[/color]

        I guess you want (\S+) ie, non-whitespace. If it are always digits you
        should use (\d+). If the number of spaces between proto and recvq can be
        more than one you should use something like:

        (\w+)\s+(\d+)

        print the values of $proto and $recvq

        Also, you can't be sure there are any recvqs{'0'} so check this
        same for protos.

        print "TCP = ...." if defined $Protos{'tcp'};
        print "RecvQ = ..." if defined $RecvQs{'0'};
        [color=blue]
        > Can a number of whitespaces be represented by:
        > /^(\w+) (\s+)/; # this is a word followed by some spaces followed by a
        > string[/color]

        nope. \s+ means one or more whitespaces. Not *string*
        and it is a word followed by exactly one space (white space?).
        See above.

        HTH

        --
        Kind regards, feel free to mail: mail(at)johnbok ma.com (or reply)
        virtual home: http://johnbokma.com/ ICQ: 218175426
        John web site hints: http://johnbokma.com/websitedesign/

        Comment

        • Ga Mu

          #19
          Re: Q: Analyse data and provide a report - Arrays?

          Troll wrote:[color=blue]
          > Here's the code extract:
          > while (<>) {
          > my($Proto)=
          > /(\s+)*$/;
          >[/color]

          Your m// above is saying find an occurence of one or more spaces, zero
          or more times, terminated by an end-of-line.
          [color=blue]
          > if (/tcp/) {
          > print 'found';
          > $Protos{$Proto} ++;[/color]

          This m// has nothing to do with the value, if any, that was extracted
          into $proto. It is looking at the last line read for "tcp".

          I'll continue in you next post...



          Comment

          • Troll

            #20
            Re: Q: Analyse data and provide a report - Arrays?

            Looks like I had some typos there but after correcting them it's still a no
            go :(
            /^(\w+) (\s+)/;
            was changed to
            /^(\w+)(\s+)(\S+ )(\s+)(\S+)/;
            # looking for word(s), 1 or more spaces, non-space(s), space(s),
            non-space(s)


            Still get the same output tho:
            #output section
            TCP = 6 # all is correct here
            Use of uninitialized value in concatenation (.) or string at... # error
            time - this refers to the 2nd print statement
            RecvQ = # this is blank

            What am I missing?


            "Troll" <abuse@microsof t.com> wrote in message
            news:%En5b.8045 2$bo1.2687@news-server.bigpond. net.au...[color=blue]
            > OK, I had some luck getting the first value incremented but no more.
            >
            > Version which works:
            > *************** **
            > if (/tcp/) {
            > my($Proto)=
            > /^(\w+)/;
            > $Protos{$Proto} ++;
            > }
            > print "TCP = $Protos{'tcp'}\ n";
            >
            > #output section
            > TCP = 6 # all is correct here
            >
            >
            > Version which does not work:
            > *************** *******
            > if (/tcp/) {
            > my($Proto, $RecvQ)=
            > /^(\w+) (\s+)/;
            > $Protos{$Proto} ++;
            > $RecvQs{$RecvQ) ++;
            > }
            > print "TCP = $Protos{'tcp'}\ n";
            > print "RecvQ = $RecvQs{'0'}\n" ;
            >
            > #output section
            > TCP = 6 # all is correct here
            > Use of uninitialized value in concatenation (.) or string at... #[/color]
            error[color=blue]
            > time - this refers to the 2nd print statement
            > RecvQ = # this is blank
            >
            > I have tried reading the second parameter as a (\s+) and as a (\d+) with[/color]
            no[color=blue]
            > luck. If you run netstat you will probably see that all items in the RecvQ
            > column are 0.
            > What have I done wrong now?
            >
            > Can a number of whitespaces be represented by:
            > /^(\w+) (\s+)/; # this is a word followed by some spaces followed by a
            > string
            > or is the above only ONE whitespace?
            >
            >[/color]


            Comment

            • Troll

              #21
              Re: Q: Analyse data and provide a report - Arrays?

              You just saved me some more stress John. Thanks !
              This (\w+)\s+(\d+) did the trick.

              Can you pls elaborate on the difference between including stuff in brackets
              or not?
              Is it always in brackets except for SPACE searches?



              "John Bokma" <postmaster@cas tleamber.com> wrote in message
              news:3f560b30$0 $184$58c7af7e@n ews.kabelfoon.n l...[color=blue]
              > Troll wrote:
              >[color=green]
              > > OK, I had some luck getting the first value incremented but no more.
              > >
              > > Version which works:
              > > *************** **
              > > if (/tcp/) {
              > > my($Proto)=
              > > /^(\w+)/;
              > > $Protos{$Proto} ++;
              > > }
              > > print "TCP = $Protos{'tcp'}\ n";
              > >
              > > #output section
              > > TCP = 6 # all is correct here
              > >
              > >
              > > Version which does not work:
              > > *************** *******
              > > if (/tcp/) {
              > > my($Proto, $RecvQ)=
              > > /^(\w+) (\s+)/;
              > > $Protos{$Proto} ++;
              > > $RecvQs{$RecvQ) ++;
              > > }
              > > print "TCP = $Protos{'tcp'}\ n";
              > > print "RecvQ = $RecvQs{'0'}\n" ;
              > >
              > > #output section
              > > TCP = 6 # all is correct here
              > > Use of uninitialized value in concatenation (.) or string at... #[/color][/color]
              error[color=blue][color=green]
              > > time - this refers to the 2nd print statement
              > > RecvQ = # this is blank
              > >
              > > I have tried reading the second parameter as a (\s+) and as a (\d+) with[/color][/color]
              no[color=blue][color=green]
              > > luck. If you run netstat you will probably see that all items in the[/color][/color]
              RecvQ[color=blue][color=green]
              > > column are 0.
              > > What have I done wrong now?[/color]
              >
              > I guess you want (\S+) ie, non-whitespace. If it are always digits you
              > should use (\d+). If the number of spaces between proto and recvq can be
              > more than one you should use something like:
              >
              > (\w+)\s+(\d+)
              >
              > print the values of $proto and $recvq
              >
              > Also, you can't be sure there are any recvqs{'0'} so check this
              > same for protos.
              >
              > print "TCP = ...." if defined $Protos{'tcp'};
              > print "RecvQ = ..." if defined $RecvQs{'0'};
              >[color=green]
              > > Can a number of whitespaces be represented by:
              > > /^(\w+) (\s+)/; # this is a word followed by some spaces followed by a
              > > string[/color]
              >
              > nope. \s+ means one or more whitespaces. Not *string*
              > and it is a word followed by exactly one space (white space?).
              > See above.
              >
              > HTH
              >
              > --
              > Kind regards, feel free to mail: mail(at)johnbok ma.com (or reply)
              > virtual home: http://johnbokma.com/ ICQ: 218175426
              > John web site hints: http://johnbokma.com/websitedesign/
              >[/color]


              Comment

              • John Bokma

                #22
                Re: Q: Analyse data and provide a report - Arrays?

                Troll wrote:
                [color=blue]
                > Looks like I had some typos there but after correcting them it's still a no
                > go :(
                > /^(\w+) (\s+)/;
                > was changed to
                > /^(\w+)(\s+)(\S+ )(\s+)(\S+)/;
                > # looking for word(s), 1 or more spaces, non-space(s), space(s),
                > non-space(s)
                >
                >
                > Still get the same output tho:
                > #output section
                > TCP = 6 # all is correct here
                > Use of uninitialized value in concatenation (.) or string at... # error
                > time - this refers to the 2nd print statement
                > RecvQ = # this is blank
                >
                > What am I missing?[/color]

                post a valid line.


                --
                Kind regards, feel free to mail: mail(at)johnbok ma.com (or reply)
                virtual home: http://johnbokma.com/ ICQ: 218175426
                John web site hints: http://johnbokma.com/websitedesign/

                Comment

                • Ga Mu

                  #23
                  Re: Q: Analyse data and provide a report - Arrays?

                  Troll wrote:
                  [color=blue]
                  > OK, I had some luck getting the first value incremented but no more.
                  >
                  > Version which works:
                  > *************** **
                  > if (/tcp/) {
                  > my($Proto)=
                  > /^(\w+)/;
                  > $Protos{$Proto} ++;
                  > }
                  > print "TCP = $Protos{'tcp'}\ n";
                  >
                  > #output section
                  > TCP = 6 # all is correct here
                  >
                  >
                  > Version which does not work:
                  > *************** *******
                  > if (/tcp/) {
                  > my($Proto, $RecvQ)=
                  > /^(\w+) (\s+)/;[/color]

                  The above re says find and extract a word into $Proto, find some
                  whitepsace and ignore it, then find more whitepsace and extract it into
                  $RecvQ. Do you mean to use an uypper-case S, meaning find non-whitepsace..?
                  [color=blue]
                  > $Protos{$Proto} ++;
                  > $RecvQs{$RecvQ) ++;
                  > }
                  > print "TCP = $Protos{'tcp'}\ n";
                  > print "RecvQ = $RecvQs{'0'}\n" ;
                  >
                  > #output section
                  > TCP = 6 # all is correct here
                  > Use of uninitialized value in concatenation (.) or string at... # error
                  > time - this refers to the 2nd print statement
                  > RecvQ = # this is blank
                  >
                  > I have tried reading the second parameter as a (\s+) and as a (\d+) with no
                  > luck. If you run netstat you will probably see that all items in the RecvQ
                  > column are 0.
                  > What have I done wrong now?
                  >[/color]

                  If you are trying to parse this:

                  Proto Recv-Q Send-Q Local Address Foreign Address State
                  tcp 0 0 redhat:ssh winxp:1099 ESTABLISHED

                  how about this:

                  my ($proto,$rxQ,$t xQ,$l_addr,$r_a ddr,$state) =
                  /^(\w+) (\d+) (\d+) (\S+) (\S+) (\w+)/;

                  which says (with whitespace in between each):

                  find a word and extract into $proto,
                  find a number and extract into $rxQ,
                  ditto for $txQ,
                  find NON-whitespace and extract into $l_addr,
                  ditto for $r_addr,
                  find a word and extract into $state.

                  Use \S+ for the addresses because they contain numbers, letters, and a
                  colon. Neither \w nor \d would match these.
                  [color=blue]
                  > Can a number of whitespaces be represented by:
                  > /^(\w+) (\s+)/; # this is a word followed by some spaces followed by a
                  > string
                  > or is the above only ONE whitespace?[/color]

                  \s (lower-case) DOES NOT mean a string, it means whitespace.
                  \S (upper-case) means non-whitespace.

                  If you have access to "The Camel Book" by ORA, try reading the section
                  on pattern matching. It's clear you're not getting how to construct a
                  meaningful regular expression.




                  Comment

                  • Ga Mu

                    #24
                    Re: Q: Analyse data and provide a report - Arrays?

                    Ga Mu wrote:[color=blue]
                    > If you are trying to parse this:
                    >
                    > Proto Recv-Q Send-Q Local Address Foreign Address State
                    > tcp 0 0 redhat:ssh winxp:1099 ESTABLISHED
                    >
                    > how about this:
                    >
                    > my ($proto,$rxQ,$t xQ,$l_addr,$r_a ddr,$state) =
                    > /^(\w+) (\d+) (\d+) (\S+) (\S+) (\w+)/;
                    >[/color]

                    WHOOPS!

                    That re should have been:

                    /^(\w+) +(\d+) +(\d+) +(\S+) +(\S+) +(\w+)/

                    I just tested it and it works (my Linux box is back up).

                    Alternatively, you could use "\s+" instead of " +". The former means
                    one or more whitespace characters (space, tab, newline) , the latter (I
                    think) means find one or more space characters only (no tab or newline).
                    [color=blue]
                    > which says (with whitespace in between each):
                    >
                    > find a word and extract into $proto,
                    > find a number and extract into $rxQ,
                    > ditto for $txQ,
                    > find NON-whitespace and extract into $l_addr,
                    > ditto for $r_addr,
                    > find a word and extract into $state.
                    >[/color]

                    Comment

                    • Troll

                      #25
                      Re: Q: Analyse data and provide a report - Arrays?

                      Greg,
                      Your last bit made me laugh cause it is exactly how I feel. Still need a lot
                      of work to understand regexes.
                      But thanks to the last posts from yourself and John and 2 links I found,
                      this is much clearer now.

                      My apologies to both of you for being such a pain :(


                      "Ga Mu" <NgamuthO@SPcom cast.netAM> wrote in message
                      news:%ao5b.3420 52$YN5.233647@s ccrnsc01...[color=blue]
                      > Troll wrote:
                      >[color=green]
                      > > OK, I had some luck getting the first value incremented but no more.
                      > >
                      > > Version which works:
                      > > *************** **
                      > > if (/tcp/) {
                      > > my($Proto)=
                      > > /^(\w+)/;
                      > > $Protos{$Proto} ++;
                      > > }
                      > > print "TCP = $Protos{'tcp'}\ n";
                      > >
                      > > #output section
                      > > TCP = 6 # all is correct here
                      > >
                      > >
                      > > Version which does not work:
                      > > *************** *******
                      > > if (/tcp/) {
                      > > my($Proto, $RecvQ)=
                      > > /^(\w+) (\s+)/;[/color]
                      >
                      > The above re says find and extract a word into $Proto, find some
                      > whitepsace and ignore it, then find more whitepsace and extract it into
                      > $RecvQ. Do you mean to use an uypper-case S, meaning find[/color]
                      non-whitepsace..?[color=blue]
                      >[color=green]
                      > > $Protos{$Proto} ++;
                      > > $RecvQs{$RecvQ) ++;
                      > > }
                      > > print "TCP = $Protos{'tcp'}\ n";
                      > > print "RecvQ = $RecvQs{'0'}\n" ;
                      > >
                      > > #output section
                      > > TCP = 6 # all is correct here
                      > > Use of uninitialized value in concatenation (.) or string at... #[/color][/color]
                      error[color=blue][color=green]
                      > > time - this refers to the 2nd print statement
                      > > RecvQ = # this is blank
                      > >
                      > > I have tried reading the second parameter as a (\s+) and as a (\d+) with[/color][/color]
                      no[color=blue][color=green]
                      > > luck. If you run netstat you will probably see that all items in the[/color][/color]
                      RecvQ[color=blue][color=green]
                      > > column are 0.
                      > > What have I done wrong now?
                      > >[/color]
                      >
                      > If you are trying to parse this:
                      >
                      > Proto Recv-Q Send-Q Local Address Foreign Address State
                      > tcp 0 0 redhat:ssh winxp:1099 ESTABLISHED
                      >
                      > how about this:
                      >
                      > my ($proto,$rxQ,$t xQ,$l_addr,$r_a ddr,$state) =
                      > /^(\w+) (\d+) (\d+) (\S+) (\S+) (\w+)/;
                      >
                      > which says (with whitespace in between each):
                      >
                      > find a word and extract into $proto,
                      > find a number and extract into $rxQ,
                      > ditto for $txQ,
                      > find NON-whitespace and extract into $l_addr,
                      > ditto for $r_addr,
                      > find a word and extract into $state.
                      >
                      > Use \S+ for the addresses because they contain numbers, letters, and a
                      > colon. Neither \w nor \d would match these.
                      >[color=green]
                      > > Can a number of whitespaces be represented by:
                      > > /^(\w+) (\s+)/; # this is a word followed by some spaces followed by a
                      > > string
                      > > or is the above only ONE whitespace?[/color]
                      >
                      > \s (lower-case) DOES NOT mean a string, it means whitespace.
                      > \S (upper-case) means non-whitespace.
                      >
                      > If you have access to "The Camel Book" by ORA, try reading the section
                      > on pattern matching. It's clear you're not getting how to construct a
                      > meaningful regular expression.
                      >
                      >
                      >
                      >[/color]


                      Comment

                      • Troll

                        #26
                        Re: Q: Analyse data and provide a report - Arrays?

                        :)

                        Mine was failing somewhere on the 4th parameter search ie. the first (\S+)
                        but that's because I was trying to do:
                        print "Dells = $LocalAddresses {'Dell'}\n;
                        whereas there was no value of Dell being passed anywhere - there was only
                        Dell:smtp and Dell:32769...DO H...


                        Also, what's the purpose of having something like this at the beginning of
                        the script:
                        my (%protos,%rxQs, %txQs,%l_addrs, %r_addrs,%state s)
                        cause when I remarked it with # there was no diff to the way the script
                        runs.



                        Comment

                        • John Bokma

                          #27
                          Re: Q: Analyse data and provide a report - Arrays?

                          Troll wrote:
                          [color=blue]
                          > Will do - thanks.
                          > I came across the
                          > use strict
                          > before [looking at other ppls script examples] but never got the chance to
                          > read up on it yet.
                          >
                          > On a TO DO list now...[/color]

                          put it on *top* of the list. Speaking of top: don't top post and snip
                          things no longer relevant.

                          --
                          Kind regards, feel free to mail: mail(at)johnbok ma.com (or reply)
                          virtual home: http://johnbokma.com/ ICQ: 218175426
                          John web site hints: http://johnbokma.com/websitedesign/

                          Comment

                          • Troll

                            #28
                            Re: Q: Analyse data and provide a report - Arrays?

                            Sorry. I tried to improve the visibility a bit as the thread was scrolling
                            but I see you point.
                            A NG etiquette refresher needed...
                            Will keep things in order now and *snip* [what does that stand for?] them
                            when necessary


                            "John Bokma" <postmaster@cas tleamber.com> wrote in message
                            news:3f561b8f$0 $203$58c7af7e@n ews.kabelfoon.n l...[color=blue]
                            > Troll wrote:
                            >[color=green]
                            > > Will do - thanks.
                            > > I came across the
                            > > use strict
                            > > before [looking at other ppls script examples] but never got the chance[/color][/color]
                            to[color=blue][color=green]
                            > > read up on it yet.
                            > >
                            > > On a TO DO list now...[/color]
                            >
                            > put it on *top* of the list. Speaking of top: don't top post and snip
                            > things no longer relevant.
                            >
                            > --
                            > Kind regards, feel free to mail: mail(at)johnbok ma.com (or reply)
                            > virtual home: http://johnbokma.com/ ICQ: 218175426
                            > John web site hints: http://johnbokma.com/websitedesign/
                            >[/color]


                            Comment

                            • John Bokma

                              #29
                              Re: Q: Analyse data and provide a report - Arrays?

                              Troll wrote:
                              [color=blue]
                              > Sorry. I tried to improve the visibility a bit as the thread was scrolling
                              > but I see you point.
                              > A NG etiquette refresher needed...
                              > Will keep things in order now and *snip* [what does that stand for?] them
                              > when necessary[/color]

                              cut. It is quite common when a large part is removed to state what was
                              removed, e.g.:

                              [cut perl example]

                              or

                              [snip perl example]

                              Sometimes <> is used instead of []. Or even ...

                              Most newsreaders provide scrolling by pressing the space bar. Reading a
                              top post and scrolling down to understand to what it is referring (and
                              back up and down etc) is always harder than reading bottom down. Most
                              postings fit on a screen after careful cutting.

                              --
                              Kind regards, feel free to mail: mail(at)johnbok ma.com (or reply)
                              virtual home: http://johnbokma.com/ ICQ: 218175426
                              John web site hints: http://johnbokma.com/websitedesign/

                              Comment

                              • Troll

                                #30
                                Re: Q: Analyse data and provide a report - Arrays?

                                John and Greg,
                                Thanks for the help today [again].

                                I'm sure to have some more Qs tomorrow but right now I need to rewrite the
                                code from my laptop to an external Solaris box. This will also mean my
                                variable definions will change. This little task will take me some time
                                especially that the vi editor I have to use is a less friendly version than
                                the one which comes with my RH9. I then need to test the code on the
                                external system so I don't see myself posting anymore today until same time
                                tomorrow.

                                Cheers,
                                T



                                "John Bokma" <postmaster@cas tleamber.com> wrote in message
                                news:3f5622c2$0 $203$58c7af7e@n ews.kabelfoon.n l...[color=blue]
                                > Troll wrote:
                                >[color=green]
                                > > Sorry. I tried to improve the visibility a bit as the thread was[/color][/color]
                                scrolling[color=blue][color=green]
                                > > but I see you point.
                                > > A NG etiquette refresher needed...
                                > > Will keep things in order now and *snip* [what does that stand for?][/color][/color]
                                them[color=blue][color=green]
                                > > when necessary[/color]
                                >
                                > cut. It is quite common when a large part is removed to state what was
                                > removed, e.g.:
                                >
                                > [cut perl example]
                                >
                                > or
                                >
                                > [snip perl example]
                                >
                                > Sometimes <> is used instead of []. Or even ...
                                >
                                > Most newsreaders provide scrolling by pressing the space bar. Reading a
                                > top post and scrolling down to understand to what it is referring (and
                                > back up and down etc) is always harder than reading bottom down. Most
                                > postings fit on a screen after careful cutting.
                                >
                                > --
                                > Kind regards, feel free to mail: mail(at)johnbok ma.com (or reply)
                                > virtual home: http://johnbokma.com/ ICQ: 218175426
                                > John web site hints: http://johnbokma.com/websitedesign/
                                >[/color]


                                Comment

                                Working...