None of our Perl experts are jumping on this, so I guess I'll give it a try. This is what I wrote:
Code:
$
$ cat readr
#! /usr/bin/perl -w
$_ = <STDIN>;
chomp;
@fields = split ',';
for ($i=0 ; $i < $#fields; $i++) {
$fields[$i] =~ s/^ *//;
print "field $i = $fields[$i] \n";
}
$j = 0;
while (<>) {
chomp;
print "line = $_ \n";
@data = split ',';
for ($i=0; $i < $#data; $i++) {
$data[$i] =~ s/^ *//;
$array = $fields[$i]; # How to combine these
$$array[$j] = $data[$i]; # two lines?
}
$j++;
}
for ($i=0 ; $i <= $#ServerName; $i++) {
print "ServerName $i = $ServerName[$i] \n";
}
$ ./readr < data1
field 0 = ServerName
field 1 = IPAddress
field 2 = Gateway
line = ServerA, 192.168.1.100, 192.168.1.1, This is some server
line = ServerB, 192.168.1.110, 192.168.1.1, This is some other server
line = ServerC, 192.168.1.120, 192.168.1.1, This is some other other server
ServerName 0 = ServerA
ServerName 1 = ServerB
ServerName 2 = ServerC
$
That last loop is just to prove that I had populated an array called ServerName. Notice the two lines that I commented. I really wanted to combine them into a single line and lose that $array scalar. But I could not find the right syntax. Can anyone kick that ball over the goalline for me?
And no fair rewriting it to use a more sensible data structure. I gave the OP what he asked for. I realize the requirements are a little odd.
Hello,
I am building an .xls file extracting info from a DB to be eventually emailed. All is good except how do I put in a header row.. like date, name of report etc. before the columns with the actual column name and data?
Thanks for any assistance.. the below is after I have signed into... (11 Replies)
So, I have a massive file with thousands of columns
I want a list of the headers in one column in another file.
So I need to strip off the top line (can use head-1)
But how can I convert from this format:
A B C D E F G
to
A
B
C
D
E
F
G (6 Replies)
Friends,
I need help with the following in UNIX.
Merge all csv files in one folder considering only 1 header row and ignoring header of all other files.
FYI - All files are in same format and contains same headers.
Thank you (4 Replies)
Hi All,
File contains header row.. we need to exclude the header row...no need to validate the first row in the file.
Data in the file should take valid data(two columns)..we need to exclude the more than two columns in the file except the first line.
email|firstname
a|123|100
b|345... (4 Replies)
Hi,
I have an input like this
1 2 3 4
2 3 4 5
4 5 6 7
I would like to count the no. of columns and print a header with a prefix "Col".
I would also like to count the no. of rows and print as first column with each line number with a prefix "Row"
So, my output would be
... (2 Replies)
Hi There!
I am saving the file count of all files in a directory to an output file using:
wc -l * > FileCount.txt
I get:
114 G4SXORD
3 G4SXORH
0 G4SXORP
117 total
But this count includes header and footer. I want to subtract 2 from the count and get
... (7 Replies)
Hi All,
i have script like ...
"TYPE_ID" "ID" "LIST_ID"
"18" "52010" "1059"
"18" "52010" "1059"
"18" "52010" "1059"
"18" "52010" "1059"
i am using the below code it's not taking the header row.
awk -F"\t" -v file=test1.txt -v file1=test2.txt ' {
if(... (7 Replies)
Hi,
So I am trying to print the first row(header) first column alongwith the matched value. But I am not sure how do I print the same, by matching a pattern located in the file
eg
File contents
Name Place
Jim NY
Jill NJ
Cathy CA
Sam TX
Daniel FL
And what I want is... (2 Replies)
I am just trying to insert the word "Index" using awk. The below is close but seems to add the word at the end and I can not get the syntax correct to add from the beginning. Thank you :).
awk -F'\t' -v OFS='\t' '{ $-1=$-1 OFS "Index"}$1=$1' file
current output
Chr Start End ... (3 Replies)
The awk below does put in VUS in the 9th field but I can not seem to skip the header then add the VUS. I tried to incorporate NR >=2 and NR > 1 with no luck. Thank you :).
input
Chr Start End Ref Alt Func.refGene PopFreqMax CLINSIG Classification
chr1 43395635 ... (5 Replies)
Discussion started by: cmccabe
5 Replies
LEARN ABOUT REDHAT
fields
fields(3pm) Perl Programmers Reference Guide fields(3pm)NAME
fields - compile-time class fields
SYNOPSIS
{
package Foo;
use fields qw(foo bar _Foo_private);
sub new {
my Foo $self = shift;
unless (ref $self) {
$self = fields::new($self);
$self->{_Foo_private} = "this is Foo's secret";
}
$self->{foo} = 10;
$self->{bar} = 20;
return $self;
}
}
my Foo $var = Foo::->new;
$var->{foo} = 42;
# this will generate a compile-time error
$var->{zap} = 42;
# subclassing
{
package Bar;
use base 'Foo';
use fields qw(baz _Bar_private); # not shared with Foo
sub new {
my $class = shift;
my $self = fields::new($class);
$self->SUPER::new(); # init base fields
$self->{baz} = 10; # init own fields
$self->{_Bar_private} = "this is Bar's secret";
return $self;
}
}
DESCRIPTION
The "fields" pragma enables compile-time verified class fields.
NOTE: The current implementation keeps the declared fields in the %FIELDS hash of the calling package, but this may change in future ver-
sions. Do not update the %FIELDS hash directly, because it must be created at compile-time for it to be fully useful, as is done by this
pragma.
If a typed lexical variable holding a reference is used to access a hash element and a package with the same name as the type has declared
class fields using this pragma, then the operation is turned into an array access at compile time.
The related "base" pragma will combine fields from base classes and any fields declared using the "fields" pragma. This enables field
inheritance to work properly.
Field names that start with an underscore character are made private to the class and are not visible to subclasses. Inherited fields can
be overridden but will generate a warning if used together with the "-w" switch.
The effect of all this is that you can have objects with named fields which are as compact and as fast arrays to access. This only works
as long as the objects are accessed through properly typed variables. If the objects are not typed, access is only checked at run time.
The following functions are supported:
new fields::new() creates and blesses a pseudo-hash comprised of the fields declared using the "fields" pragma into the specified
class. This makes it possible to write a constructor like this:
package Critter::Sounds;
use fields qw(cat dog bird);
sub new {
my Critter::Sounds $self = shift;
$self = fields::new($self) unless ref $self;
$self->{cat} = 'meow'; # scalar element
@$self{'dog','bird'} = ('bark','tweet'); # slice
return $self;
}
phash fields::phash() can be used to create and initialize a plain (unblessed) pseudo-hash. This function should always be used instead
of creating pseudo-hashes directly.
If the first argument is a reference to an array, the pseudo-hash will be created with keys from that array. If a second argument
is supplied, it must also be a reference to an array whose elements will be used as the values. If the second array contains less
elements than the first, the trailing elements of the pseudo-hash will not be initialized. This makes it particularly useful for
creating a pseudo-hash from subroutine arguments:
sub dogtag {
my $tag = fields::phash([qw(name rank ser_num)], [@_]);
}
fields::phash() also accepts a list of key-value pairs that will be used to construct the pseudo hash. Examples:
my $tag = fields::phash(name => "Joe",
rank => "captain",
ser_num => 42);
my $pseudohash = fields::phash(%args);
SEE ALSO
base, "Pseudo-hashes: Using an array as a hash" in perlref
perl v5.8.0 2002-06-01 fields(3pm)