Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl (http://www.velocityreviews.com/forums/f17-perl.html)
-   -   Q: Analyse data and provide a report - Arrays? (http://www.velocityreviews.com/forums/t24333-q-analyse-data-and-provide-a-report-arrays.html)

Troll 08-30-2003 01:20 PM

Q: Analyse data and provide a report - Arrays?
 
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 08-31-2003 12:16 AM

Re: Q: Analyse data and provide a report - Arrays?
 
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" <NgamuthO@SPcomcast.netAM> 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 08-31-2003 01:38 PM

Re: Q: Analyse data and provide a report - Arrays?
 
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/perlw...pter/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" <abuse@microsoft.com> wrote in message
news:e7b4b.74021$bo1.27093@news-server.bigpond.net.au...
> 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" <NgamuthO@SPcomcast.netAM> 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
> >

>
>




Ga Mu 08-31-2003 08:13 PM

Re: Q: Analyse data and provide a report - Arrays?
 
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/perlw...pter/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 09-01-2003 01:04 AM

Re: Q: Analyse data and provide a report - Arrays?
 
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" <NgamuthO@SPcomcast.netAM> wrote in message
news:3Gs4b.141469$2x.40819@rwcrnsc52.ops.asp.att.n et...
> 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/perlw...pter/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 09-01-2003 03:46 PM

Re: Q: Analyse data and provide a report - Arrays?
 
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 09-01-2003 04:54 PM

Re: Q: Analyse data and provide a report - Arrays?
 
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" <NgamuthO@SPcomcast.netAM> wrote in message
news:uRJ4b.147542$2x.41412@rwcrnsc52.ops.asp.att.n et...
> 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 09-02-2003 10:28 AM

Re: Q: Analyse data and provide a report - Arrays?
 
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" <abuse@microsoft.com> wrote in message
news:uRK4b.77094$bo1.13700@news-server.bigpond.net.au...
> 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" <NgamuthO@SPcomcast.netAM> wrote in message
> news:uRJ4b.147542$2x.41412@rwcrnsc52.ops.asp.att.n et...
> > 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 09-02-2003 02:16 PM

Re: Q: Analyse data and provide a report - Arrays?
 
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" <abuse@microsoft.com> wrote in message
news:eh_4b.78248$bo1.24286@news-server.bigpond.net.au...
> 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" <abuse@microsoft.com> wrote in message
> news:uRK4b.77094$bo1.13700@news-server.bigpond.net.au...
> > 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" <NgamuthO@SPcomcast.netAM> wrote in message
> > news:uRJ4b.147542$2x.41412@rwcrnsc52.ops.asp.att.n et...
> > > 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 09-02-2003 03:35 PM

Re: Q: Analyse data and provide a report - Arrays?
 
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...
>
>




All times are GMT. The time now is 05:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.