02-09-2007
array problem
Dear Experts,
please help me out once again my array concepts is not very clear i have one text file like.
1|usa|hh
2|usa|ll
3|usa|vg
4|uk|nn
5|uk|bb
6|kuwait|mm
6|kuwait|jkj
7|dubai|hh
i want to store the third fied of a text file in he array and after that it should give me some reports like this
third field count
usa 3
uk 2
kuwait 2
dubai 1
it should store the third field in to the array and give me the count how many times any name appeared.the third field can be alphanumeric.
please can any body tell me how to do this using array.
Regards,
Shary
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
hi,
I get a *.dat files list in an array using:
array=($(find . -name "*.dat"))
the problem is that when a filename contains spaces, each space-separated token of the filename is in a different element of array.
For instance if I have:
x@x:~/tmp$ ls *.dat
test1.dat test 2.dat ... (1 Reply)
Discussion started by: jul
1 Replies
2. Shell Programming and Scripting
I am using /bin/ksh for this problem.
I have created some arrays with variable names as the array names:
cnt=1
{ while read myline; do
tempmeas="${meas%%;*}"
cto="${meas#*;}"
tempstream=$stream
# wholemeas holds the name of the array
# each array name... (0 Replies)
Discussion started by: ajgwin
0 Replies
3. Shell Programming and Scripting
hi i am trying to perform some calculations with awk and arrays. i have this so far:
awk 'NR==FNR{ for(i=1; i<=NF; i++) {array+=$i} tot++;next}
{for(i=1; i<=NF; i++) {avg=array/tot} {diff=(array - avg)}} {for(i=1; i<=NF; i++) {printf("%5.8f\n",diff)}}' "$count".txt "$count".ttt >... (4 Replies)
Discussion started by: npatwardhan
4 Replies
4. UNIX for Dummies Questions & Answers
Hi all,
I would like to declare a vector of variables and access them sequentially.
Here is my code
ARRAY_CT="0001000000 0000100000 0000010000"
ELEMENTS_CT=${#ARRAY_CT}
echo $ELEMENTS_CT
for (( j=1;j<=$ELEMENTS_IS;j++)); do
echo ${ARRAY_IS}
done
... (2 Replies)
Discussion started by: f_o_555
2 Replies
5. Programming
i have no idea how to make a text file
abc efg
hij klm
nop qrs
to be a array such as, arr to be "abc efg" arr "hij kml" etc..... in C (2 Replies)
Discussion started by: tyckelvin1
2 Replies
6. Shell Programming and Scripting
Hi all,
I am working in ubuntu for past few weeks .Since I was working in debian I had no problem with arrays.I followed the same method in ubuntu,but is is not working as I expected.
Name="apple"
Name="orange"
print ${Name}
Expected result is apple.But I got a error as "Bad... (8 Replies)
Discussion started by: karthigayan
8 Replies
7. Emergency UNIX and Linux Support
Below is my script. This script is getting an error code such as this one.
fileListener.bat: entityArray=craig.uss@pnc.com: not found
craig.uss@pnc.com
fileListener.bat: entityArray=duns_noncusts.txt: not found
duns_noncusts.txt
fileListener.bat: entityArray=duns_misc.cpy: not found... (4 Replies)
Discussion started by: mkjp
4 Replies
8. Shell Programming and Scripting
I've got this problem, if I modify an array in the loop and print it, everything is fine as long as I stay in the loop. But, when I print it outside the loop, nothing happens... How can I solve this problem?
Here I prepared a sample for you to see my problem;
zgrw@Rain:~$ cat test
asd
123... (4 Replies)
Discussion started by: zgrw
4 Replies
9. Shell Programming and Scripting
I am trying to map values in the input file, where 2nd column depends on the specific value in the 1st column. When 1st column is A place 1 into 2nd column, when it is B, place 2, when C place 3, otherwise no change.
My input:
U |100|MAIN ST |CLMN1|1
A |200|GREEN LN |CLMN2|2
1 |12... (4 Replies)
Discussion started by: migurus
4 Replies
10. Shell Programming and Scripting
Hi, I have the following problem that is beyond what I can currently do with bash scripting.
In file 1, I have ~ 2500000 values. Note this file is not sorted.
3 19 LABEL_A
3 37 LABEL_B
2 12 LABEL_C
1 15 LABEL_D
I have a list of values in "file 2" ~ 25000 unique lines:
Note -... (6 Replies)
Discussion started by: hubleo
6 Replies
LEARN ABOUT OSX
moose::cookbook::basics::person_buildargsandbuild
Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD(3) User Contributed Perl Documentation Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD(3)
NAME
Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD - Using BUILDARGS and BUILD to hook into object construction
VERSION
version 2.0604
SYNOPSIS
package Person;
has 'ssn' => (
is => 'ro',
isa => 'Str',
predicate => 'has_ssn',
);
has 'country_of_residence' => (
is => 'ro',
isa => 'Str',
default => 'usa'
);
has 'first_name' => (
is => 'ro',
isa => 'Str',
);
has 'last_name' => (
is => 'ro',
isa => 'Str',
);
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
if ( @_ == 1 && ! ref $_[0] ) {
return $class->$orig(ssn => $_[0]);
}
else {
return $class->$orig(@_);
}
};
sub BUILD {
my $self = shift;
if ( $self->country_of_residence eq 'usa' ) {
die 'Cannot create a Person who lives in the USA without an ssn.'
unless $self->has_ssn;
}
}
DESCRIPTION
This recipe demonstrates the use of "BUILDARGS" and "BUILD". By defining these methods, we can hook into the object construction process
without overriding "new".
The "BUILDARGS" method is called before an object has been created. It is called as a class method, and receives all of the parameters
passed to the "new" method. It is expected to do something with these arguments and return a hash reference. The keys of the hash must be
attribute "init_arg"s.
The primary purpose of "BUILDARGS" is to allow a class to accept something other than named arguments. In the case of our "Person" class,
we are allowing it to be called with a single argument, a social security number:
my $person = Person->new('123-45-6789');
The key part of our "BUILDARGS" is this conditional:
if ( @_ == 1 && ! ref $_[0] ) {
return $class->$orig(ssn => $_[0]);
}
By default, Moose constructors accept a list of key-value pairs, or a hash reference. We need to make sure that $_[0] is not a reference
before assuming it is a social security number.
We call the original "BUILDARGS" method to handle all the other cases. You should always do this in your own "BUILDARGS" methods, since
Moose::Object provides its own "BUILDARGS" method that handles hash references and a list of key-value pairs.
The "BUILD" method is called after the object is constructed, but before it is returned to the caller. The "BUILD" method provides an
opportunity to check the object state as a whole. This is a good place to put logic that cannot be expressed as a type constraint on a
single attribute.
In the "Person" class, we need to check the relationship between two attributes, "ssn" and "country_of_residence". We throw an exception if
the object is not logically consistent.
MORE CONSIDERATIONS
This recipe is made significantly simpler because all of the attributes are read-only. If the "country_of_residence" attribute were
settable, we would need to check that a Person had an "ssn" if the new country was "usa". This could be done with a "before" modifier.
CONCLUSION
We have repeatedly discouraged overriding "new" in Moose classes. This recipe shows how you can use "BUILDARGS" and "BUILD" to hook into
object construction without overriding "new".
The "BUILDARGS" method lets us expand on Moose's built-in parameter handling for constructors. The "BUILD" method lets us implement logical
constraints across the whole object after it is created.
AUTHOR
Moose is maintained by the Moose Cabal, along with the help of many contributors. See "CABAL" in Moose and "CONTRIBUTORS" in Moose for
details.
COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Infinity Interactive, Inc..
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
perl v5.16.2 2012-09-19 Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD(3)