Q: Analyse data and provide a report - Arrays?

Discussion in 'Perl' started by Troll, Aug 30, 2003.

  1. Troll

    Troll Guest

    Hi,

    I need to write a script which reads some data and reports the findings.
    Just to give you an idea the structure is similar to the following.

    Data input example:

    HEADING 1
    **********
    ColumnA ColumnB ColumnC ColumnD ColumnE
    Pete Male Marketing Single 40
    Kate Female Marketing Married 30
    John Male Sales Married 38
    Pete Male Sales Single 52
    John Male Sales Single 24


    HEADING 2
    **********
    ColumnF ColumnG ColumnH ColumnI
    whatever
    whatever
    whatever
    whatever


    Report Output example:
    # of Pete's =
    # of Males =
    # of Salespeople =
    # of Singles =
    # of over 35s =


    Since this is the first time I'm even writing such a script I would
    appreciate some pointers.
    1) Do I use arrays or associate arrays for this? Why or why not?
    2) Is it possible for someone to give me a code example of counting how many
    Singles we have?
    3) What happens when I have read all the data under HEADING 1 and need to
    move onto HEADING 2?
    That is, how do I accomplish the jump from what I think is one loop onto the
    next?

    I imagine that there will be many more posts following this one so there's
    no need to get into too much detail. Some guidance would be nice as I will
    need to utilise Google and my references for the rest.

    Thanks in advance.
     
    Troll, Aug 30, 2003
    #1
    1. Advertising

  2. Troll

    Troll Guest

    Ga Mu,
    Great stuff - thanks very much. :)

    The headings differentiate blocks of data so once we count everything under
    HEADING 1 we move onto HEADING 2 then HEADING 3 etc.

    Does this help a bit?




    "Ga Mu" <> wrote in message
    news:xl44b.306860$Ho3.43264@sccrnsc03...
    > Troll wrote:
    >
    > > 1) Do I use arrays or associate arrays for this? Why or why not?

    >
    > Use hashes (aka associative arrays) because they work so well for
    > counting occurences of words. A hash instance is automatically
    > initialized to zero the first time it is used, so, assuming you have
    > already declared the hash %names ('my %names;') and we are in your
    > parsing loop and have extracted the person's name into $name, all you
    > need is:
    >
    > $names{$name}++; # increment the count for this name.
    >
    > > 2) Is it possible for someone to give me a code example of counting how

    many
    > > Singles we have?

    >
    > You could count everything with hashes.
    >
    > Prior to your parsing loop:
    >
    > my (%names, %sexes, %depts, %m_statuses, %ages);
    >
    > Within your parsing loop:
    >
    > # extract four words and a number into scalars:
    > my ($name, $sex, $dept, $m_status, $age) =
    > /^(\w+) (\w+) (\w+) (\w+) (\d+)$/;
    >
    > # increment counts for each:
    > $names{$name}++;
    > $sexes{$sex}++;
    > $depts{$dept}++;
    > $m_statuses{$m_status}++;
    > $ages{$age}++;
    >
    > After your parsing loop:
    >
    > $names{'Pete'} gives the number of Petes.
    > $sexes{'Male'} gives the number of Males.
    > $depts{'Sales'} gives the number of sales people.
    > $m_statuses{'Single'} gives the number of single people.
    > $ages{'25'} gives the number of 25 year-olds.
    >
    > To print a list of all names and the number of occurences of each:
    >
    > foreach $key (keys %names) {
    > print "$key: $names{$key}\n";
    > }
    >
    > This will output something like:
    >
    > John: 2
    > Pete: 3
    > Kate: 1
    >
    > This list could have been sorted by either name or count. Do a 'perldoc
    > -f' for 'keys' and 'sort'.
    >
    > > 3) What happens when I have read all the data under HEADING 1 and need

    to
    > > move onto HEADING 2?
    > > That is, how do I accomplish the jump from what I think is one loop onto

    the
    > > next?

    >
    > Can't answer that, as you don't provide enough detail. What is the
    > significance of the headings? Would the results be the same if the
    > headings were completely ignored or do the headings signify some
    > distinction between blocks of data?
    >
    > Greg
    >
     
    Troll, Aug 31, 2003
    #2
    1. Advertising

  3. Troll

    Troll Guest

    Ga Mu,

    Pls disregard last post.

    With regard to the jump between HEADINGS, will it be enough to do something
    like:
    while (<>)
    ....
    if (/HEADING 1/ .. /HEADING 2/) {
    # line falls between HEADING 1 and HEADING 2 in the text, inclusive.
    # then do the string extraction
    # then increment stuff
    elsif (/HEADING 2/ .. /HEADING 3/) {
    # line falls between HEADING 2 and HEADING 3 in the text, inclusive.
    # then do the string extraction
    # then increment stuff
    etc?

    I quite like the code example you provided - actually found a similar one in
    http://www.oreilly.com/catalog/perlwsmng/chapter/ch08.html
    Up until now I was under the impression that I would have to use split - can
    you elaborate why you chose a different approach?

    One other task I have to do is similar to:
    > If a line contains Single in the column then get the single person's name.

    I sort of came up with:
    foreach $m_statuses{'Single'}
    print $names{$name}

    but that's probably totally wrong. Can you advise?

    Thanks again.


    "Troll" <> wrote in message
    news:e7b4b.74021$...
    > Ga Mu,
    > Great stuff - thanks very much. :)
    >
    > The headings differentiate blocks of data so once we count everything

    under
    > HEADING 1 we move onto HEADING 2 then HEADING 3 etc.
    >
    > Does this help a bit?
    >
    >
    >
    >
    > "Ga Mu" <> wrote in message
    > news:xl44b.306860$Ho3.43264@sccrnsc03...
    > > Troll wrote:
    > >
    > > > 1) Do I use arrays or associate arrays for this? Why or why not?

    > >
    > > Use hashes (aka associative arrays) because they work so well for
    > > counting occurences of words. A hash instance is automatically
    > > initialized to zero the first time it is used, so, assuming you have
    > > already declared the hash %names ('my %names;') and we are in your
    > > parsing loop and have extracted the person's name into $name, all you
    > > need is:
    > >
    > > $names{$name}++; # increment the count for this name.
    > >
    > > > 2) Is it possible for someone to give me a code example of counting

    how
    > many
    > > > Singles we have?

    > >
    > > You could count everything with hashes.
    > >
    > > Prior to your parsing loop:
    > >
    > > my (%names, %sexes, %depts, %m_statuses, %ages);
    > >
    > > Within your parsing loop:
    > >
    > > # extract four words and a number into scalars:
    > > my ($name, $sex, $dept, $m_status, $age) =
    > > /^(\w+) (\w+) (\w+) (\w+) (\d+)$/;
    > >
    > > # increment counts for each:
    > > $names{$name}++;
    > > $sexes{$sex}++;
    > > $depts{$dept}++;
    > > $m_statuses{$m_status}++;
    > > $ages{$age}++;
    > >
    > > After your parsing loop:
    > >
    > > $names{'Pete'} gives the number of Petes.
    > > $sexes{'Male'} gives the number of Males.
    > > $depts{'Sales'} gives the number of sales people.
    > > $m_statuses{'Single'} gives the number of single people.
    > > $ages{'25'} gives the number of 25 year-olds.
    > >
    > > To print a list of all names and the number of occurences of each:
    > >
    > > foreach $key (keys %names) {
    > > print "$key: $names{$key}\n";
    > > }
    > >
    > > This will output something like:
    > >
    > > John: 2
    > > Pete: 3
    > > Kate: 1
    > >
    > > This list could have been sorted by either name or count. Do a 'perldoc
    > > -f' for 'keys' and 'sort'.
    > >
    > > > 3) What happens when I have read all the data under HEADING 1 and need

    > to
    > > > move onto HEADING 2?
    > > > That is, how do I accomplish the jump from what I think is one loop

    onto
    > the
    > > > next?

    > >
    > > Can't answer that, as you don't provide enough detail. What is the
    > > significance of the headings? Would the results be the same if the
    > > headings were completely ignored or do the headings signify some
    > > distinction between blocks of data?
    > >
    > > Greg
    > >

    >
    >
     
    Troll, Aug 31, 2003
    #3
  4. Troll

    Ga Mu Guest

    Troll wrote:
    > Ga Mu,
    >
    > Pls disregard last post.
    >
    > With regard to the jump between HEADINGS, will it be enough to do something
    > like:
    > while (<>)
    > ...
    > if (/HEADING 1/ .. /HEADING 2/) {
    > # line falls between HEADING 1 and HEADING 2 in the text, inclusive.
    > # then do the string extraction
    > # then increment stuff
    > elsif (/HEADING 2/ .. /HEADING 3/) {
    > # line falls between HEADING 2 and HEADING 3 in the text, inclusive.
    > # then do the string extraction
    > # then increment stuff
    > etc?


    I am unclear as to the distinction between blocks. Are there a separate
    group of totals for each heading or is everyting totalled up together?
    If the latter, then simply ignore the headings. If the former, then you
    could parse out the heading name and use a multidimensional hash. I.e.,
    replace this:

    $names{$name}++;

    with this:

    $names{$heading}{$name}++;

    >
    > I quite like the code example you provided - actually found a similar one in
    > http://www.oreilly.com/catalog/perlwsmng/chapter/ch08.html
    > Up until now I was under the impression that I would have to use split - can
    > you elaborate why you chose a different approach?


    Either method produces the same results. If you plan on incorporating
    error checking, m// allows to specifically define a format, e.g., four
    words and a number, whereas split simply breaks a string up into a list.
    Whichever method makes you happy.

    > One other task I have to do is similar to:
    >
    >>If a line contains Single in the column then get the single person's name.

    >
    > I sort of came up with:
    > foreach $m_statuses{'Single'}
    > print $names{$name}
    >
    > but that's probably totally wrong. Can you advise?


    Yes, it is totally wrong. $m_statuses{'Single'} is a scalar. It is the
    count of lines where the marital status is 'Single'. Your foreach loop
    above would produce a syntax error. Although it is not what you're
    after, a valid foreach loop could look like this:

    foreach $m_status ( keys %m_statuses ) {
    #
    # $m_status will be 'female' for one iteration of the loop and 'male'
    # for the other. (Unless you have more than two sexes...)
    #
    }

    Perhaps a more meaningful foreach loop would look like this:

    foreach $age ( keys %ages ) {
    #
    # For each iteration, $age will one the ages that was found in the data
    # -->> IN NO PARTICULAR ORDER <<-- unless you sort it.
    #
    }

    To do what you propose, i.e., print the name of all single people, you
    would have to include the logic for that in the parsing loop:

    # extract four words and a number into scalars:
    my ($name, $sex, $dept, $m_status, $age) =
    /^(\w+) (\w+) (\w+) (\w+) (\d+)$/;

    # increment counts for each:
    $names{$name}++;
    $sexes{$sex}++;
    $depts{$dept}++;
    $m_statuses{$m_status}++;
    $ages{$age}++;

    # take special actions:
    if ( $m_status eq 'Single' ) print "$name is single.\n";
    if ( $age >= 40 ) print "$name is over the hill!\n";


    Hope this helps!

    Greg
     
    Ga Mu, Aug 31, 2003
    #4
  5. Troll

    Troll Guest

    Thanks again !

    1)
    Sorry for being too vague. With regard to the HEADINGS they separate blocks
    of data. But because the column names will be different [data is different]
    then I'm not quite sure I could use:
    $names{$heading}{$name}++;

    So I'm looking at creating separate my () definitions for each HEADING and
    just wanted to confirm how to jump out of one HEADING loop and start with
    the next.

    For example, under HEADING 1 we have these columns:
    Name, Sex, Dept, M_Status, Age

    and under HEADING 2we have:
    Address, Phone#, Mobile#, Salary

    So at the beginning of the script I would have
    my (%names, %sexes, %depts, %m_statuses, %ages)
    my (%addresses, %phones, %mobiles, %salaries)
    #then I have my while (<>) and parsing here
    #I have my output at the end

    Is that a little more clearer?


    2)
    With my last question regarding the printing of the names of single people,
    if we include a print statement in the parsing loop would that give us
    something like:
    Pete is single.
    John is single.
    while the parsing is still running?

    What I'm after is hopefully feeding that output into something else
    [@array?] which can then print a list of the names [line by line] at the end
    of the script, something like:
    #this is the output structure
    Number of Petes =
    Number of Males =
    Singles are:
    Pete
    John
    Number of Salespeople =


    Does this make sense?

    Thanks Greg.


    "Ga Mu" <> wrote in message
    news:3Gs4b.141469$...
    > Troll wrote:
    > > Ga Mu,
    > >
    > > Pls disregard last post.
    > >
    > > With regard to the jump between HEADINGS, will it be enough to do

    something
    > > like:
    > > while (<>)
    > > ...
    > > if (/HEADING 1/ .. /HEADING 2/) {
    > > # line falls between HEADING 1 and HEADING 2 in the text, inclusive.
    > > # then do the string extraction
    > > # then increment stuff
    > > elsif (/HEADING 2/ .. /HEADING 3/) {
    > > # line falls between HEADING 2 and HEADING 3 in the text, inclusive.
    > > # then do the string extraction
    > > # then increment stuff
    > > etc?

    >
    > I am unclear as to the distinction between blocks. Are there a separate
    > group of totals for each heading or is everyting totalled up together?
    > If the latter, then simply ignore the headings. If the former, then you
    > could parse out the heading name and use a multidimensional hash. I.e.,
    > replace this:
    >
    > $names{$name}++;
    >
    > with this:
    >
    > $names{$heading}{$name}++;
    >
    > >
    > > I quite like the code example you provided - actually found a similar

    one in
    > > http://www.oreilly.com/catalog/perlwsmng/chapter/ch08.html
    > > Up until now I was under the impression that I would have to use split -

    can
    > > you elaborate why you chose a different approach?

    >
    > Either method produces the same results. If you plan on incorporating
    > error checking, m// allows to specifically define a format, e.g., four
    > words and a number, whereas split simply breaks a string up into a list.
    > Whichever method makes you happy.
    >
    > > One other task I have to do is similar to:
    > >
    > >>If a line contains Single in the column then get the single person's

    name.
    > >
    > > I sort of came up with:
    > > foreach $m_statuses{'Single'}
    > > print $names{$name}
    > >
    > > but that's probably totally wrong. Can you advise?

    >
    > Yes, it is totally wrong. $m_statuses{'Single'} is a scalar. It is the
    > count of lines where the marital status is 'Single'. Your foreach loop
    > above would produce a syntax error. Although it is not what you're
    > after, a valid foreach loop could look like this:
    >
    > foreach $m_status ( keys %m_statuses ) {
    > #
    > # $m_status will be 'female' for one iteration of the loop and 'male'
    > # for the other. (Unless you have more than two sexes...)
    > #
    > }
    >
    > Perhaps a more meaningful foreach loop would look like this:
    >
    > foreach $age ( keys %ages ) {
    > #
    > # For each iteration, $age will one the ages that was found in the data
    > # -->> IN NO PARTICULAR ORDER <<-- unless you sort it.
    > #
    > }
    >
    > To do what you propose, i.e., print the name of all single people, you
    > would have to include the logic for that in the parsing loop:
    >
    > # extract four words and a number into scalars:
    > my ($name, $sex, $dept, $m_status, $age) =
    > /^(\w+) (\w+) (\w+) (\w+) (\d+)$/;
    >
    > # increment counts for each:
    > $names{$name}++;
    > $sexes{$sex}++;
    > $depts{$dept}++;
    > $m_statuses{$m_status}++;
    > $ages{$age}++;
    >
    > # take special actions:
    > if ( $m_status eq 'Single' ) print "$name is single.\n";
    > if ( $age >= 40 ) print "$name is over the hill!\n";
    >
    >
    > Hope this helps!
    >
    > Greg
    >
    >
     
    Troll, Sep 1, 2003
    #5
  6. Troll

    Ga Mu Guest

    Troll wrote:
    > Thanks again !
    >
    > 1)
    > Sorry for being too vague. With regard to the HEADINGS they separate blocks
    > of data. But because the column names will be different [data is different]
    > then I'm not quite sure I could use:
    > $names{$heading}{$name}++;
    >
    > So I'm looking at creating separate my () definitions for each HEADING and
    > just wanted to confirm how to jump out of one HEADING loop and start with
    > the next.
    >
    > For example, under HEADING 1 we have these columns:
    > Name, Sex, Dept, M_Status, Age
    >
    > and under HEADING 2we have:
    > Address, Phone#, Mobile#, Salary
    >
    > So at the beginning of the script I would have
    > my (%names, %sexes, %depts, %m_statuses, %ages)
    > my (%addresses, %phones, %mobiles, %salaries)
    > #then I have my while (<>) and parsing here
    > #I have my output at the end
    >
    > Is that a little more clearer?


    Yes. Much clearer. There are a couple of different ways you could do
    this. One is to use a single loop that reads through the file and uses
    a state variable (e.g., $heading) to keep track of where you are in the
    parsing process. The other is to have a separate loop for each heading.
    Again, six of one, half a dozen of another. It's more a matter of
    preference than anything else.

    An example of the first approach:

    my $heading = 'initial';
    my $fin_name = '/usr/local/blah/blah/blah';
    open FIN,$fin_name || die "Can't open $fin_name\n";

    while (<FIN>) {

    # check for a new heading
    # I am assuming single word heading names
    if ( /HEADING (\S+)/ {

    $heading = $1; # set $heading equal to word extracted above

    # take appropriate action based on the heading we are under

    } elsif ( $heading eq 'NAMES' ) {

    ( $name, $sex, $dept, $m_status, $age ) =
    /(\w+) (\w+) (\w+) (\w+) (\d+)/;

    # update counts, append to lists, etc...

    } elsif ( $heading eq 'ADDRESSES' ) {

    # I am assuming the address field is limited to 30 characters
    # here:
    ( $address,$phone, $mobile, $salary ) =
    /(\.{30}) (\S+) (\S+) (\d+)/;

    # update counts, append to lists, etc...

    }

    }


    And the second approach:

    my $heading = 'initial';
    my $fin_name = '/usr/local/blah/blah/blah';
    open FIN,$fin_name || die "Can't open $fin_name\n";

    # scan for first heading
    while ( <FIN> && ! /HEADING NAMES/ );

    # parse the names, etc...
    while ( <FIN> && ! /HEADING ADDRESSES/ ) {

    ( $name, $sex, $dept, $m_status, $age ) =
    /(\w+) (\w+) (\w+) (\w+) (\d+)/;

    # update counts, append to lists, etc...


    # parse the addresses, etc...
    # for brevity , I am assuming only two headings
    while ( <FIN> ) {

    ( $address,$phone, $mobile, $salary ) =
    /(\.{30}) (\S+) (\S+) (\d+)/;

    # update counts, append to lists, etc...

    }

    >
    >
    > 2)
    > With my last question regarding the printing of the names of single people,
    > if we include a print statement in the parsing loop would that give us
    > something like:
    > Pete is single.
    > John is single.
    > while the parsing is still running?


    Yes.

    >
    > What I'm after is hopefully feeding that output into something else
    > [@array?] which can then print a list of the names [line by line] at the end
    > of the script, something like:
    > #this is the output structure
    > Number of Petes =
    > Number of Males =
    > Singles are:
    > Pete
    > John
    > Number of Salespeople =
    >
    >
    > Does this make sense?
    >


    Yes. It would be easy to create a list/array of, e.g., single people.
    Prior to the loop, declare the array. Within the loop, test each person
    for being single. If they are, push them onto the list:

    # prior to your parsing loop, declare array @singles:

    my @singles;

    # within your parsing loop, after parsing out name, status, etc.:

    if ( $m_status eq 'Single' ) push @singles,($name);

    # after loop, to print the list of singles:

    print "Single persons:\n";
    foreach $single_person ( @singles ) print " $single_person\n";


    Greg
     
    Ga Mu, Sep 1, 2003
    #6
  7. Troll

    Troll Guest

    Wow. I don't know how you get the time to respond to my queries in such
    detail. It is greatly appreciated.
    I just came back from work and it's like 2:30 am so I'll crash out soon and
    have a closer read tomorrow [especially of the HEADINGS part].

    With the push @array stuff I actually got to this today in my readings. I
    saw an example of appending an array onto another array with a push and I
    was wondering if we could just substitute a $variable for one of the arrays.
    I'm glad you confirmed this. :)

    I was also wondering if doing this at the beginning of the script:

    my (%names, %sexes, %depts, %m_statuses, %ages) # declaring things
    locally

    would be considered bad practice. I thought that one should declare things
    as my ( ) if one is using things within a loop so as not to impact anything
    external to the loop. But if one uses variables/arrays both within and
    outside the loops, should we then still declare stuff as my ( )?
    Maybe I'm just confused about my ( )...

    Greg, if you could possibly keep an eye on this thread for the next few days
    I would be very much in your debt. Your help has been invaluabe so far in
    allowing me to visualise quite a few things.

    Thanks very much.


    "Ga Mu" <> wrote in message
    news:uRJ4b.147542$...
    > Troll wrote:
    > > Thanks again !
    > >
    > > 1)
    > > Sorry for being too vague. With regard to the HEADINGS they separate

    blocks
    > > of data. But because the column names will be different [data is

    different]
    > > then I'm not quite sure I could use:
    > > $names{$heading}{$name}++;
    > >
    > > So I'm looking at creating separate my () definitions for each HEADING

    and
    > > just wanted to confirm how to jump out of one HEADING loop and start

    with
    > > the next.
    > >
    > > For example, under HEADING 1 we have these columns:
    > > Name, Sex, Dept, M_Status, Age
    > >
    > > and under HEADING 2we have:
    > > Address, Phone#, Mobile#, Salary
    > >
    > > So at the beginning of the script I would have
    > > my (%names, %sexes, %depts, %m_statuses, %ages)
    > > my (%addresses, %phones, %mobiles, %salaries)
    > > #then I have my while (<>) and parsing here
    > > #I have my output at the end
    > >
    > > Is that a little more clearer?

    >
    > Yes. Much clearer. There are a couple of different ways you could do
    > this. One is to use a single loop that reads through the file and uses
    > a state variable (e.g., $heading) to keep track of where you are in the
    > parsing process. The other is to have a separate loop for each heading.
    > Again, six of one, half a dozen of another. It's more a matter of
    > preference than anything else.
    >
    > An example of the first approach:
    >
    > my $heading = 'initial';
    > my $fin_name = '/usr/local/blah/blah/blah';
    > open FIN,$fin_name || die "Can't open $fin_name\n";
    >
    > while (<FIN>) {
    >
    > # check for a new heading
    > # I am assuming single word heading names
    > if ( /HEADING (\S+)/ {
    >
    > $heading = $1; # set $heading equal to word extracted above
    >
    > # take appropriate action based on the heading we are under
    >
    > } elsif ( $heading eq 'NAMES' ) {
    >
    > ( $name, $sex, $dept, $m_status, $age ) =
    > /(\w+) (\w+) (\w+) (\w+) (\d+)/;
    >
    > # update counts, append to lists, etc...
    >
    > } elsif ( $heading eq 'ADDRESSES' ) {
    >
    > # I am assuming the address field is limited to 30 characters
    > # here:
    > ( $address,$phone, $mobile, $salary ) =
    > /(\.{30}) (\S+) (\S+) (\d+)/;
    >
    > # update counts, append to lists, etc...
    >
    > }
    >
    > }
    >
    >
    > And the second approach:
    >
    > my $heading = 'initial';
    > my $fin_name = '/usr/local/blah/blah/blah';
    > open FIN,$fin_name || die "Can't open $fin_name\n";
    >
    > # scan for first heading
    > while ( <FIN> && ! /HEADING NAMES/ );
    >
    > # parse the names, etc...
    > while ( <FIN> && ! /HEADING ADDRESSES/ ) {
    >
    > ( $name, $sex, $dept, $m_status, $age ) =
    > /(\w+) (\w+) (\w+) (\w+) (\d+)/;
    >
    > # update counts, append to lists, etc...
    >
    >
    > # parse the addresses, etc...
    > # for brevity , I am assuming only two headings
    > while ( <FIN> ) {
    >
    > ( $address,$phone, $mobile, $salary ) =
    > /(\.{30}) (\S+) (\S+) (\d+)/;
    >
    > # update counts, append to lists, etc...
    >
    > }
    >
    > >
    > >
    > > 2)
    > > With my last question regarding the printing of the names of single

    people,
    > > if we include a print statement in the parsing loop would that give us
    > > something like:
    > > Pete is single.
    > > John is single.
    > > while the parsing is still running?

    >
    > Yes.
    >
    > >
    > > What I'm after is hopefully feeding that output into something else
    > > [@array?] which can then print a list of the names [line by line] at the

    end
    > > of the script, something like:
    > > #this is the output structure
    > > Number of Petes =
    > > Number of Males =
    > > Singles are:
    > > Pete
    > > John
    > > Number of Salespeople =
    > >
    > >
    > > Does this make sense?
    > >

    >
    > Yes. It would be easy to create a list/array of, e.g., single people.
    > Prior to the loop, declare the array. Within the loop, test each person
    > for being single. If they are, push them onto the list:
    >
    > # prior to your parsing loop, declare array @singles:
    >
    > my @singles;
    >
    > # within your parsing loop, after parsing out name, status, etc.:
    >
    > if ( $m_status eq 'Single' ) push @singles,($name);
    >
    > # after loop, to print the list of singles:
    >
    > print "Single persons:\n";
    > foreach $single_person ( @singles ) print " $single_person\n";
    >
    >
    > Greg
    >
     
    Troll, Sep 1, 2003
    #7
  8. Troll

    Troll Guest

    Now time for some stupid Qs:

    Let's say that the data I have is in a file called employees.
    How can I call this file so that I can parse it?

    1) Can I do:
    @HRdata = `cat employees`;
    while (<@HRdata>) {


    2) With regard to the HEADING sections, the script has to be able to
    recognise the different sections by the following rules:
    # there's a blank line
    before each heading
    HEADING 1 # this is the name of the heading -
    this is a string with a special character and a blank space as part of it
    ColumnA ColumnB ColumnC # these are the column names - these are
    strings which also can inlude a blank space if they have 2 or more words
    ******* # a sort of an underlining
    pattern

    I guess this is to make sure that one does not include any silly heading
    data as part of the arrays created and the parsing only takes place on
    'real' data. Can you pls advise? Or do you need more info? I'm more in
    favour of creating separate 'if' loops due to my 'newbie' status. I'll get
    lost otherwise...

    Thanks.



    "Troll" <> wrote in message
    news:uRK4b.77094$...
    > Wow. I don't know how you get the time to respond to my queries in such
    > detail. It is greatly appreciated.
    > I just came back from work and it's like 2:30 am so I'll crash out soon

    and
    > have a closer read tomorrow [especially of the HEADINGS part].
    >
    > With the push @array stuff I actually got to this today in my readings. I
    > saw an example of appending an array onto another array with a push and I
    > was wondering if we could just substitute a $variable for one of the

    arrays.
    > I'm glad you confirmed this. :)
    >
    > I was also wondering if doing this at the beginning of the script:
    >
    > my (%names, %sexes, %depts, %m_statuses, %ages) # declaring things
    > locally
    >
    > would be considered bad practice. I thought that one should declare things
    > as my ( ) if one is using things within a loop so as not to impact

    anything
    > external to the loop. But if one uses variables/arrays both within and
    > outside the loops, should we then still declare stuff as my ( )?
    > Maybe I'm just confused about my ( )...
    >
    > Greg, if you could possibly keep an eye on this thread for the next few

    days
    > I would be very much in your debt. Your help has been invaluabe so far in
    > allowing me to visualise quite a few things.
    >
    > Thanks very much.
    >
    >
    > "Ga Mu" <> wrote in message
    > news:uRJ4b.147542$...
    > > Troll wrote:
    > > > Thanks again !
    > > >
    > > > 1)
    > > > Sorry for being too vague. With regard to the HEADINGS they separate

    > blocks
    > > > of data. But because the column names will be different [data is

    > different]
    > > > then I'm not quite sure I could use:
    > > > $names{$heading}{$name}++;
    > > >
    > > > So I'm looking at creating separate my () definitions for each HEADING

    > and
    > > > just wanted to confirm how to jump out of one HEADING loop and start

    > with
    > > > the next.
    > > >
    > > > For example, under HEADING 1 we have these columns:
    > > > Name, Sex, Dept, M_Status, Age
    > > >
    > > > and under HEADING 2we have:
    > > > Address, Phone#, Mobile#, Salary
    > > >
    > > > So at the beginning of the script I would have
    > > > my (%names, %sexes, %depts, %m_statuses, %ages)
    > > > my (%addresses, %phones, %mobiles, %salaries)
    > > > #then I have my while (<>) and parsing here
    > > > #I have my output at the end
    > > >
    > > > Is that a little more clearer?

    > >
    > > Yes. Much clearer. There are a couple of different ways you could do
    > > this. One is to use a single loop that reads through the file and uses
    > > a state variable (e.g., $heading) to keep track of where you are in the
    > > parsing process. The other is to have a separate loop for each heading.
    > > Again, six of one, half a dozen of another. It's more a matter of
    > > preference than anything else.
    > >
    > > An example of the first approach:
    > >
    > > my $heading = 'initial';
    > > my $fin_name = '/usr/local/blah/blah/blah';
    > > open FIN,$fin_name || die "Can't open $fin_name\n";
    > >
    > > while (<FIN>) {
    > >
    > > # check for a new heading
    > > # I am assuming single word heading names
    > > if ( /HEADING (\S+)/ {
    > >
    > > $heading = $1; # set $heading equal to word extracted above
    > >
    > > # take appropriate action based on the heading we are under
    > >
    > > } elsif ( $heading eq 'NAMES' ) {
    > >
    > > ( $name, $sex, $dept, $m_status, $age ) =
    > > /(\w+) (\w+) (\w+) (\w+) (\d+)/;
    > >
    > > # update counts, append to lists, etc...
    > >
    > > } elsif ( $heading eq 'ADDRESSES' ) {
    > >
    > > # I am assuming the address field is limited to 30 characters
    > > # here:
    > > ( $address,$phone, $mobile, $salary ) =
    > > /(\.{30}) (\S+) (\S+) (\d+)/;
    > >
    > > # update counts, append to lists, etc...
    > >
    > > }
    > >
    > > }
    > >
    > >
    > > And the second approach:
    > >
    > > my $heading = 'initial';
    > > my $fin_name = '/usr/local/blah/blah/blah';
    > > open FIN,$fin_name || die "Can't open $fin_name\n";
    > >
    > > # scan for first heading
    > > while ( <FIN> && ! /HEADING NAMES/ );
    > >
    > > # parse the names, etc...
    > > while ( <FIN> && ! /HEADING ADDRESSES/ ) {
    > >
    > > ( $name, $sex, $dept, $m_status, $age ) =
    > > /(\w+) (\w+) (\w+) (\w+) (\d+)/;
    > >
    > > # update counts, append to lists, etc...
    > >
    > >
    > > # parse the addresses, etc...
    > > # for brevity , I am assuming only two headings
    > > while ( <FIN> ) {
    > >
    > > ( $address,$phone, $mobile, $salary ) =
    > > /(\.{30}) (\S+) (\S+) (\d+)/;
    > >
    > > # update counts, append to lists, etc...
    > >
    > > }
    > >
    > > >
    > > >
    > > > 2)
    > > > With my last question regarding the printing of the names of single

    > people,
    > > > if we include a print statement in the parsing loop would that give us
    > > > something like:
    > > > Pete is single.
    > > > John is single.
    > > > while the parsing is still running?

    > >
    > > Yes.
    > >
    > > >
    > > > What I'm after is hopefully feeding that output into something else
    > > > [@array?] which can then print a list of the names [line by line] at

    the
    > end
    > > > of the script, something like:
    > > > #this is the output structure
    > > > Number of Petes =
    > > > Number of Males =
    > > > Singles are:
    > > > Pete
    > > > John
    > > > Number of Salespeople =
    > > >
    > > >
    > > > Does this make sense?
    > > >

    > >
    > > Yes. It would be easy to create a list/array of, e.g., single people.
    > > Prior to the loop, declare the array. Within the loop, test each person
    > > for being single. If they are, push them onto the list:
    > >
    > > # prior to your parsing loop, declare array @singles:
    > >
    > > my @singles;
    > >
    > > # within your parsing loop, after parsing out name, status, etc.:
    > >
    > > if ( $m_status eq 'Single' ) push @singles,($name);
    > >
    > > # after loop, to print the list of singles:
    > >
    > > print "Single persons:\n";
    > > foreach $single_person ( @singles ) print " $single_person\n";
    > >
    > >
    > > Greg
    > >

    >
    >
     
    Troll, Sep 2, 2003
    #8
  9. Troll

    Troll Guest

    I'm getting heaps of the following errors when I run my script:
    Use of uninitialized value in hash element at ...

    The beginning of my script looks like:
    my(%names, %sexes, %depts);
    %names = ("name" => "0");
    %sexes = ("sex" => "0");
    %depts = ("dept" => "0");

    $names = '0';
    $sexes = '0';
    $depts = '0';
    $name = '0';
    $sex = '0';
    $dept = '0';

    while (<>)
    #and the parsing loop here...


    The hash errors relate to only these 3 lines which are part of the parsing
    loop:
    $names{$name}++;
    $sexes{$sex}++;
    $depts{$dept}++;


    Can you run over the variable declarations/initializations for me as I'm not
    sure I'm doing this right?
    Thanks.


    "Troll" <> wrote in message
    news:eh_4b.78248$...
    > Now time for some stupid Qs:
    >
    > Let's say that the data I have is in a file called employees.
    > How can I call this file so that I can parse it?
    >
    > 1) Can I do:
    > @HRdata = `cat employees`;
    > while (<@HRdata>) {
    >
    >
    > 2) With regard to the HEADING sections, the script has to be able to
    > recognise the different sections by the following rules:
    > # there's a blank

    line
    > before each heading
    > HEADING 1 # this is the name of the

    heading -
    > this is a string with a special character and a blank space as part of it
    > ColumnA ColumnB ColumnC # these are the column names - these are
    > strings which also can inlude a blank space if they have 2 or more words
    > ******* # a sort of an underlining
    > pattern
    >
    > I guess this is to make sure that one does not include any silly heading
    > data as part of the arrays created and the parsing only takes place on
    > 'real' data. Can you pls advise? Or do you need more info? I'm more in
    > favour of creating separate 'if' loops due to my 'newbie' status. I'll get
    > lost otherwise...
    >
    > Thanks.
    >
    >
    >
    > "Troll" <> wrote in message
    > news:uRK4b.77094$...
    > > Wow. I don't know how you get the time to respond to my queries in such
    > > detail. It is greatly appreciated.
    > > I just came back from work and it's like 2:30 am so I'll crash out soon

    > and
    > > have a closer read tomorrow [especially of the HEADINGS part].
    > >
    > > With the push @array stuff I actually got to this today in my readings.

    I
    > > saw an example of appending an array onto another array with a push and

    I
    > > was wondering if we could just substitute a $variable for one of the

    > arrays.
    > > I'm glad you confirmed this. :)
    > >
    > > I was also wondering if doing this at the beginning of the script:
    > >
    > > my (%names, %sexes, %depts, %m_statuses, %ages) # declaring

    things
    > > locally
    > >
    > > would be considered bad practice. I thought that one should declare

    things
    > > as my ( ) if one is using things within a loop so as not to impact

    > anything
    > > external to the loop. But if one uses variables/arrays both within and
    > > outside the loops, should we then still declare stuff as my ( )?
    > > Maybe I'm just confused about my ( )...
    > >
    > > Greg, if you could possibly keep an eye on this thread for the next few

    > days
    > > I would be very much in your debt. Your help has been invaluabe so far

    in
    > > allowing me to visualise quite a few things.
    > >
    > > Thanks very much.
    > >
    > >
    > > "Ga Mu" <> wrote in message
    > > news:uRJ4b.147542$...
    > > > Troll wrote:
    > > > > Thanks again !
    > > > >
    > > > > 1)
    > > > > Sorry for being too vague. With regard to the HEADINGS they separate

    > > blocks
    > > > > of data. But because the column names will be different [data is

    > > different]
    > > > > then I'm not quite sure I could use:
    > > > > $names{$heading}{$name}++;
    > > > >
    > > > > So I'm looking at creating separate my () definitions for each

    HEADING
    > > and
    > > > > just wanted to confirm how to jump out of one HEADING loop and start

    > > with
    > > > > the next.
    > > > >
    > > > > For example, under HEADING 1 we have these columns:
    > > > > Name, Sex, Dept, M_Status, Age
    > > > >
    > > > > and under HEADING 2we have:
    > > > > Address, Phone#, Mobile#, Salary
    > > > >
    > > > > So at the beginning of the script I would have
    > > > > my (%names, %sexes, %depts, %m_statuses, %ages)
    > > > > my (%addresses, %phones, %mobiles, %salaries)
    > > > > #then I have my while (<>) and parsing here
    > > > > #I have my output at the end
    > > > >
    > > > > Is that a little more clearer?
    > > >
    > > > Yes. Much clearer. There are a couple of different ways you could do
    > > > this. One is to use a single loop that reads through the file and

    uses
    > > > a state variable (e.g., $heading) to keep track of where you are in

    the
    > > > parsing process. The other is to have a separate loop for each

    heading.
    > > > Again, six of one, half a dozen of another. It's more a matter of
    > > > preference than anything else.
    > > >
    > > > An example of the first approach:
    > > >
    > > > my $heading = 'initial';
    > > > my $fin_name = '/usr/local/blah/blah/blah';
    > > > open FIN,$fin_name || die "Can't open $fin_name\n";
    > > >
    > > > while (<FIN>) {
    > > >
    > > > # check for a new heading
    > > > # I am assuming single word heading names
    > > > if ( /HEADING (\S+)/ {
    > > >
    > > > $heading = $1; # set $heading equal to word extracted above
    > > >
    > > > # take appropriate action based on the heading we are under
    > > >
    > > > } elsif ( $heading eq 'NAMES' ) {
    > > >
    > > > ( $name, $sex, $dept, $m_status, $age ) =
    > > > /(\w+) (\w+) (\w+) (\w+) (\d+)/;
    > > >
    > > > # update counts, append to lists, etc...
    > > >
    > > > } elsif ( $heading eq 'ADDRESSES' ) {
    > > >
    > > > # I am assuming the address field is limited to 30 characters
    > > > # here:
    > > > ( $address,$phone, $mobile, $salary ) =
    > > > /(\.{30}) (\S+) (\S+) (\d+)/;
    > > >
    > > > # update counts, append to lists, etc...
    > > >
    > > > }
    > > >
    > > > }
    > > >
    > > >
    > > > And the second approach:
    > > >
    > > > my $heading = 'initial';
    > > > my $fin_name = '/usr/local/blah/blah/blah';
    > > > open FIN,$fin_name || die "Can't open $fin_name\n";
    > > >
    > > > # scan for first heading
    > > > while ( <FIN> && ! /HEADING NAMES/ );
    > > >
    > > > # parse the names, etc...
    > > > while ( <FIN> && ! /HEADING ADDRESSES/ ) {
    > > >
    > > > ( $name, $sex, $dept, $m_status, $age ) =
    > > > /(\w+) (\w+) (\w+) (\w+) (\d+)/;
    > > >
    > > > # update counts, append to lists, etc...
    > > >
    > > >
    > > > # parse the addresses, etc...
    > > > # for brevity , I am assuming only two headings
    > > > while ( <FIN> ) {
    > > >
    > > > ( $address,$phone, $mobile, $salary ) =
    > > > /(\.{30}) (\S+) (\S+) (\d+)/;
    > > >
    > > > # update counts, append to lists, etc...
    > > >
    > > > }
    > > >
    > > > >
    > > > >
    > > > > 2)
    > > > > With my last question regarding the printing of the names of single

    > > people,
    > > > > if we include a print statement in the parsing loop would that give

    us
    > > > > something like:
    > > > > Pete is single.
    > > > > John is single.
    > > > > while the parsing is still running?
    > > >
    > > > Yes.
    > > >
    > > > >
    > > > > What I'm after is hopefully feeding that output into something else
    > > > > [@array?] which can then print a list of the names [line by line] at

    > the
    > > end
    > > > > of the script, something like:
    > > > > #this is the output structure
    > > > > Number of Petes =
    > > > > Number of Males =
    > > > > Singles are:
    > > > > Pete
    > > > > John
    > > > > Number of Salespeople =
    > > > >
    > > > >
    > > > > Does this make sense?
    > > > >
    > > >
    > > > Yes. It would be easy to create a list/array of, e.g., single people.
    > > > Prior to the loop, declare the array. Within the loop, test each

    person
    > > > for being single. If they are, push them onto the list:
    > > >
    > > > # prior to your parsing loop, declare array @singles:
    > > >
    > > > my @singles;
    > > >
    > > > # within your parsing loop, after parsing out name, status, etc.:
    > > >
    > > > if ( $m_status eq 'Single' ) push @singles,($name);
    > > >
    > > > # after loop, to print the list of singles:
    > > >
    > > > print "Single persons:\n";
    > > > foreach $single_person ( @singles ) print " $single_person\n";
    > > >
    > > >
    > > > Greg
    > > >

    > >
    > >

    >
    >
     
    Troll, Sep 2, 2003
    #9
  10. Troll

    Ga Mu Guest

    Troll wrote:
    > Greg,
    > I decided to give you a glimpse at the code itself so as to make it clearer.
    > Just be aware that the variable/array names have changed but the general
    > idea is the same.
    > The hash errors refer to the variables in the increment section.
    >
    > #!/usr/bin/perl -w
    >
    > open(NET, "netstat|") || die ("Cannot run netstat: $!");
    >
    > my(%UDP4localaddresses, %UDP4remoteaddresses, %UDP4states);
    >
    > $UDP4localaddress = '0';
    > $UDP4remoteaddress = '0';
    > $UDP4state = '0';
    >


    Why are you doing this (above)? This is initializing three variables to
    zero. These three variables have nothing to do with the three variables
    of the same name in the while loop.

    > $UDP4localaddresses = '0';
    > $UDP4remoteaddresses = '0';
    > $UDP4states = '0';
    >


    Why are you doing this (above)? This is initializing three scalars to
    zero. These three scalars have the same name, but have nothing else to
    do with the hashes of the same name.

    > $UDP4localaddresses{$UDP4localaddress} = '0';
    > $UDP4remoteaddresses{$UDP4remoteaddress} = '0';
    > $UDP4states = ($UDP4state} = '0';
    >


    Instances of hash keys are automatically initialized to zero. That is
    what makes them perfect for counting occurences of unknown words,
    numbers, etc. And even if you had to initialize them, you are
    initilizing $UDP4localaddresses{0} to zero.

    > while (<NET>) {
    > my($UDP4localaddress, $UDP4remoteaddress, $UDP4state)=
    > /(\s+) (\s+) (\s+)$/;
    >
    > #increments start here
    > $UDP4localaddresses{$UDP4localaddress}++;
    > $UDP4remoteaddresses{$UDP4remoteaddress}++;
    > $UDP4states = ($UDP4state}++;


    If the increments above are failing, it is probably because your m// is
    failing and one or more of the keys (variable inside the {}) are
    undefined. Try putting a print statement before the increments and
    print each of the variables you are extracting, then play with the
    regular expression until you get values for ALL of them.

    > }
    >
    > #here comes the output
    >
    >
    > Can you pls criticise my futile attempt to get this going? As one can see,
    > I'm not that clear on initializations...
    >
    >
     
    Ga Mu, Sep 2, 2003
    #10
  11. Troll

    Ga Mu Guest

    Troll wrote:

    > Now time for some stupid Qs:
    >
    > Let's say that the data I have is in a file called employees.
    > How can I call this file so that I can parse it?
    >
    > 1) Can I do:
    > @HRdata = `cat employees`;
    > while (<@HRdata>) {


    The above is considered bad practice, especially if the file is large.
    Why read the entire file into memory when you can read, process, and
    discard a line at a time..? To open and read a file:

    open (FIN, '<employess') || die "blah blah blah...";

    while (<FIN>) {


    }

    >
    >
    > 2) With regard to the HEADING sections, the script has to be able to
    > recognise the different sections by the following rules:
    > # there's a blank line
    > before each heading
    > HEADING 1 # this is the name of the heading -
    > this is a string with a special character and a blank space as part of it
    > ColumnA ColumnB ColumnC # these are the column names - these are
    > strings which also can inlude a blank space if they have 2 or more words
    > ******* # a sort of an underlining
    > pattern
    >


    while (<FIN>) {

    if ( /^$/ ) {

    # this is a blank line, don't do anything

    } elsif ( /HEADING (\.+)/ ) {

    # this is a heading, with the heading name in $1

    } elsif ( (($name, $sex, $status, $age) = /(\s+) (\s+) (\s+) (\d+)/) ==
    4 ) {

    # this line contains three words and a number, do whatever
    # (I'm not really sure if this will work. My Linux box is
    # down and I have no way of testing.)

    }

    } # end of while(<FIN>)

    > I guess this is to make sure that one does not include any silly heading
    > data as part of the arrays created and the parsing only takes place on
    > 'real' data. Can you pls advise? Or do you need more info? I'm more in
    > favour of creating separate 'if' loops due to my 'newbie' status. I'll get
    > lost otherwise...
    >


    "if loops"...? How does one make an if loop?

    > Thanks.
    >
    >
    >
    > "Troll" <> wrote in message
    > news:uRK4b.77094$...
    >
    >>Wow. I don't know how you get the time to respond to my queries in such
    >>detail. It is greatly appreciated.
    >>I just came back from work and it's like 2:30 am so I'll crash out soon

    >
    > and
    >
    >>have a closer read tomorrow [especially of the HEADINGS part].
    >>
    >>With the push @array stuff I actually got to this today in my readings. I
    >>saw an example of appending an array onto another array with a push and I
    >>was wondering if we could just substitute a $variable for one of the

    >
    > arrays.
    >
    >>I'm glad you confirmed this. :)
    >>
    >>I was also wondering if doing this at the beginning of the script:
    >>
    >>my (%names, %sexes, %depts, %m_statuses, %ages) # declaring things
    >>locally
    >>
    >>would be considered bad practice. I thought that one should declare things
    >>as my ( ) if one is using things within a loop so as not to impact

    >
    > anything
    >
    >>external to the loop. But if one uses variables/arrays both within and
    >>outside the loops, should we then still declare stuff as my ( )?
    >>Maybe I'm just confused about my ( )...
    >>
    >>Greg, if you could possibly keep an eye on this thread for the next few

    >
    > days
    >
    >>I would be very much in your debt. Your help has been invaluabe so far in
    >>allowing me to visualise quite a few things.
    >>
    >>Thanks very much.
    >>
    >>
    >>"Ga Mu" <> wrote in message
    >>news:uRJ4b.147542$...
    >>
    >>>Troll wrote:
    >>>
    >>>>Thanks again !
    >>>>
    >>>>1)
    >>>>Sorry for being too vague. With regard to the HEADINGS they separate

    >>
    >>blocks
    >>
    >>>>of data. But because the column names will be different [data is

    >>
    >>different]
    >>
    >>>>then I'm not quite sure I could use:
    >>>>$names{$heading}{$name}++;
    >>>>
    >>>>So I'm looking at creating separate my () definitions for each HEADING

    >>
    >>and
    >>
    >>>>just wanted to confirm how to jump out of one HEADING loop and start

    >>
    >>with
    >>
    >>>>the next.
    >>>>
    >>>>For example, under HEADING 1 we have these columns:
    >>>>Name, Sex, Dept, M_Status, Age
    >>>>
    >>>>and under HEADING 2we have:
    >>>>Address, Phone#, Mobile#, Salary
    >>>>
    >>>>So at the beginning of the script I would have
    >>>>my (%names, %sexes, %depts, %m_statuses, %ages)
    >>>>my (%addresses, %phones, %mobiles, %salaries)
    >>>>#then I have my while (<>) and parsing here
    >>>>#I have my output at the end
    >>>>
    >>>>Is that a little more clearer?
    >>>
    >>>Yes. Much clearer. There are a couple of different ways you could do
    >>>this. One is to use a single loop that reads through the file and uses
    >>>a state variable (e.g., $heading) to keep track of where you are in the
    >>>parsing process. The other is to have a separate loop for each heading.
    >>> Again, six of one, half a dozen of another. It's more a matter of
    >>>preference than anything else.
    >>>
    >>>An example of the first approach:
    >>>
    >>>my $heading = 'initial';
    >>>my $fin_name = '/usr/local/blah/blah/blah';
    >>>open FIN,$fin_name || die "Can't open $fin_name\n";
    >>>
    >>>while (<FIN>) {
    >>>
    >>> # check for a new heading
    >>> # I am assuming single word heading names
    >>> if ( /HEADING (\S+)/ {
    >>>
    >>> $heading = $1; # set $heading equal to word extracted above
    >>>
    >>> # take appropriate action based on the heading we are under
    >>>
    >>> } elsif ( $heading eq 'NAMES' ) {
    >>>
    >>> ( $name, $sex, $dept, $m_status, $age ) =
    >>> /(\w+) (\w+) (\w+) (\w+) (\d+)/;
    >>>
    >>> # update counts, append to lists, etc...
    >>>
    >>> } elsif ( $heading eq 'ADDRESSES' ) {
    >>>
    >>> # I am assuming the address field is limited to 30 characters
    >>> # here:
    >>> ( $address,$phone, $mobile, $salary ) =
    >>> /(\.{30}) (\S+) (\S+) (\d+)/;
    >>>
    >>> # update counts, append to lists, etc...
    >>>
    >>> }
    >>>
    >>>}
    >>>
    >>>
    >>>And the second approach:
    >>>
    >>>my $heading = 'initial';
    >>>my $fin_name = '/usr/local/blah/blah/blah';
    >>>open FIN,$fin_name || die "Can't open $fin_name\n";
    >>>
    >>># scan for first heading
    >>>while ( <FIN> && ! /HEADING NAMES/ );
    >>>
    >>># parse the names, etc...
    >>>while ( <FIN> && ! /HEADING ADDRESSES/ ) {
    >>>
    >>> ( $name, $sex, $dept, $m_status, $age ) =
    >>> /(\w+) (\w+) (\w+) (\w+) (\d+)/;
    >>>
    >>> # update counts, append to lists, etc...
    >>>
    >>>
    >>># parse the addresses, etc...
    >>># for brevity , I am assuming only two headings
    >>>while ( <FIN> ) {
    >>>
    >>> ( $address,$phone, $mobile, $salary ) =
    >>> /(\.{30}) (\S+) (\S+) (\d+)/;
    >>>
    >>> # update counts, append to lists, etc...
    >>>
    >>>}
    >>>
    >>>
    >>>>
    >>>>2)
    >>>>With my last question regarding the printing of the names of single

    >>
    >>people,
    >>
    >>>>if we include a print statement in the parsing loop would that give us
    >>>>something like:
    >>>>Pete is single.
    >>>>John is single.
    >>>>while the parsing is still running?
    >>>
    >>>Yes.
    >>>
    >>>
    >>>>What I'm after is hopefully feeding that output into something else
    >>>>[@array?] which can then print a list of the names [line by line] at

    >
    > the
    >
    >>end
    >>
    >>>>of the script, something like:
    >>>>#this is the output structure
    >>>>Number of Petes =
    >>>>Number of Males =
    >>>>Singles are:
    >>>>Pete
    >>>>John
    >>>>Number of Salespeople =
    >>>>
    >>>>
    >>>>Does this make sense?
    >>>>
    >>>
    >>>Yes. It would be easy to create a list/array of, e.g., single people.
    >>>Prior to the loop, declare the array. Within the loop, test each person
    >>>for being single. If they are, push them onto the list:
    >>>
    >>># prior to your parsing loop, declare array @singles:
    >>>
    >>>my @singles;
    >>>
    >>># within your parsing loop, after parsing out name, status, etc.:
    >>>
    >>>if ( $m_status eq 'Single' ) push @singles,($name);
    >>>
    >>># after loop, to print the list of singles:
    >>>
    >>>print "Single persons:\n";
    >>>foreach $single_person ( @singles ) print " $single_person\n";
    >>>
    >>>
    >>>Greg
    >>>

    >>
    >>

    >
    >
     
    Ga Mu, Sep 2, 2003
    #11
  12. Troll

    Troll Guest

    Thanks again :)

    Will I get these errors:
    Use of uninitialized value in print at ./netstat.pl line 16, <NET> line 1.
    Use of uninitialized value in print at ./netstat.pl line 17, <NET> line 1.
    Use of uninitialized value in print at ./netstat.pl line 18, <NET> line 1.
    ....etc

    if an undefined value is passed, for example, to $UDP4localaddress?
    Because if that's the case then all I need to do is to make sure that
    whatever I'm passing as part of the m()// is correctly split and defined as
    a string, digit, word etc, yes?


    "Ga Mu" <> wrote in message
    news:SM25b.251663$cF.79266@rwcrnsc53...
    > Troll wrote:
    > > Greg,
    > > I decided to give you a glimpse at the code itself so as to make it

    clearer.
    > > Just be aware that the variable/array names have changed but the general
    > > idea is the same.
    > > The hash errors refer to the variables in the increment section.
    > >
    > > #!/usr/bin/perl -w
    > >
    > > open(NET, "netstat|") || die ("Cannot run netstat: $!");
    > >
    > > my(%UDP4localaddresses, %UDP4remoteaddresses, %UDP4states);
    > >
    > > $UDP4localaddress = '0';
    > > $UDP4remoteaddress = '0';
    > > $UDP4state = '0';
    > >

    >
    > Why are you doing this (above)? This is initializing three variables to
    > zero. These three variables have nothing to do with the three variables
    > of the same name in the while loop.
    >
    > > $UDP4localaddresses = '0';
    > > $UDP4remoteaddresses = '0';
    > > $UDP4states = '0';
    > >

    >
    > Why are you doing this (above)? This is initializing three scalars to
    > zero. These three scalars have the same name, but have nothing else to
    > do with the hashes of the same name.
    >
    > > $UDP4localaddresses{$UDP4localaddress} = '0';
    > > $UDP4remoteaddresses{$UDP4remoteaddress} = '0';
    > > $UDP4states = ($UDP4state} = '0';
    > >

    >
    > Instances of hash keys are automatically initialized to zero. That is
    > what makes them perfect for counting occurences of unknown words,
    > numbers, etc. And even if you had to initialize them, you are
    > initilizing $UDP4localaddresses{0} to zero.
    >
    > > while (<NET>) {
    > > my($UDP4localaddress, $UDP4remoteaddress, $UDP4state)=
    > > /(\s+) (\s+) (\s+)$/;
    > >
    > > #increments start here
    > > $UDP4localaddresses{$UDP4localaddress}++;
    > > $UDP4remoteaddresses{$UDP4remoteaddress}++;
    > > $UDP4states = ($UDP4state}++;

    >
    > If the increments above are failing, it is probably because your m// is
    > failing and one or more of the keys (variable inside the {}) are
    > undefined. Try putting a print statement before the increments and
    > print each of the variables you are extracting, then play with the
    > regular expression until you get values for ALL of them.
    >
    > > }
    > >
    > > #here comes the output
    > >
    > >
    > > Can you pls criticise my futile attempt to get this going? As one can

    see,
    > > I'm not that clear on initializations...
    > >
    > >

    >
     
    Troll, Sep 2, 2003
    #12
  13. Troll

    John Bokma Guest

    Ga Mu wrote:


    > while (<FIN>) {
    >
    > if ( /^$/ ) {
    >
    > # this is a blank line, don't do anything



    next if /^\s*$/; # skip blank lines (or consisting of white space
    # only)

    > } elsif ( /HEADING (\.+)/ ) {
    >
    > # this is a heading, with the heading name in $1



    if (/ .....) {

    # this is a heading
    next;
    }

    > } elsif ( (($name, $sex, $status, $age) = /(\s+) (\s+) (\s+) (\d+)/) ==



    if (......) {

    # bla bla
    next;
    }

    next moves on to the next "while step".

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
     
    John Bokma, Sep 2, 2003
    #13
  14. Troll

    Ga Mu Guest

    Troll wrote:

    > Thanks again :)
    >
    > Will I get these errors:
    > Use of uninitialized value in print at ./netstat.pl line 16, <NET> line 1.
    > Use of uninitialized value in print at ./netstat.pl line 17, <NET> line 1.
    > Use of uninitialized value in print at ./netstat.pl line 18, <NET> line 1.
    > ...etc
    >
    > if an undefined value is passed, for example, to $UDP4localaddress?
    > Because if that's the case then all I need to do is to make sure that
    > whatever I'm passing as part of the m()// is correctly split and defined as
    > a string, digit, word etc, yes?
    >


    Exactly. Experiment with your re in the m// until you get values.
     
    Ga Mu, Sep 2, 2003
    #14
  15. Troll

    Ga Mu Guest

    Troll wrote:

    > Thanks again. No reading files into memory from now on [unless necessary] :)
    >
    > The data will actually be read from stdin in the form of
    > $ netstat | netstat.pl
    > or
    > $ netstat.pl < netstat
    >
    > Will something like this suffice?
    > #!/usr/bin/perl -w
    > while (<STDIN>) {


    STDIN is the default file handle, so all you need is:

    while (<>) {

    }

    >>"if loops"...? How does one make an if loop?

    >
    > What I meant here is that I'll create 4 separate 'if' sections [with their
    > own elsif branches], one for each HEADING section [there are 4 of them].
    > So I think I meant 'if' statements...is that better or I am still confusing
    > my terminology?


    Makes more sense...
     
    Ga Mu, Sep 2, 2003
    #15
  16. Troll

    Troll Guest

    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 ?
     
    Troll, Sep 2, 2003
    #16
  17. Troll

    Troll Guest

    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?
     
    Troll, Sep 3, 2003
    #17
  18. Troll

    John Bokma Guest

    Troll wrote:

    > 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?


    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'};

    > Can a number of whitespaces be represented by:
    > /^(\w+) (\s+)/; # this is a word followed by some spaces followed by a
    > string


    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)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
     
    John Bokma, Sep 3, 2003
    #18
  19. Troll

    Ga Mu Guest

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


    Your m// above is saying find an occurence of one or more spaces, zero
    or more times, terminated by an end-of-line.

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


    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...
     
    Ga Mu, Sep 3, 2003
    #19
  20. Troll

    Troll Guest

    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" <> wrote in message
    news:%En5b.80452$...
    > 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?
    >
    >
     
    Troll, Sep 3, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    0
    Views:
    380
  2. Rupert Woodman

    Analyse which classes are used

    Rupert Woodman, Dec 15, 2005, in forum: Java
    Replies:
    5
    Views:
    3,320
    Rupert Woodman
    Dec 16, 2005
  3. Johan Holst Nielsen

    Analyse of PDF (or EPS?)

    Johan Holst Nielsen, Nov 20, 2003, in forum: Python
    Replies:
    13
    Views:
    707
    David Boddie
    Nov 25, 2003
  4. Vusi
    Replies:
    5
    Views:
    449
    John Machin
    Jul 4, 2006
  5. JB
    Replies:
    2
    Views:
    1,826
Loading...

Share This Page