Hello all;
I'm having trouble with my perl script and need some guidance. I am able to read data from a .csv file using the script and am placing it into an array @field. The data consists of four columns. I'm using the first column $field[0] and the fourth column $field[3], and placing that into a hash.
My problem is that I want to be able to use the data in $field[1], to reference the keys in the hash. $field[0] consists of number 1 - 4, and $field[1] consist of number 1-4 in random order.
I want my output to be the data in $field[3] corresponding to $field[0] (its row).
I've tried looping through the hash using :
while (($key,$value) = each(%hash), and have only been successful to output the data in $field[3] when $key is "= =" to $field[1].
Hope I explained the problem well enough, if any further explanation is necessary please let me know. I also have a portion of my code it necessary.
Thanks
6 1794 numberwhun 3,509
Recognized Expert Moderator Specialist
It would be a huge help if you could do two things:
1. Provide us with your script, as you have it so far. This will allow us to see all of your code, including modules that you may be using.
2. A sample of the file you are parsing with the script so we can see the data in question as well. Even if you don't post real data, it should be in the format the real data is.
Thanks!
Jeff
Sure no problem, just to warn you the code is a little messy because I've tried so many things, most of it is commented out just incase I need to reference back to it:
The data im using looks similar to this, but this is an example:
column 1 column 2 column 3
1 3 dog
2 2 cat
3 4 mouse
4 1 hat
I want the output to be outputed to a .csv file and look like this:
column 1 column 2 column 3 column 4
1 3 dog mouse
2 2 cat cat
3 4 mouse hat
4 1 hat dog
Thanks -
#!/usr/bin/perl
-
#use warnings;
-
-
open(INFILE,"perl_data.csv") || die ("Could not open file");
-
open(OUT, ">test.csv") || die("Could not write file");
-
open(IN, "test.csv") || die("Could not open file");
-
-
-
my @field =(0);
-
my $field;
-
-
my @created_array = (0 .. 4);
-
-
while (<INFILE>) {
-
s/\n//;
-
@field =split(",");
-
-
my %hash = ($field[0],$field[3]);
-
-
-
# while ( my ($key, $value) = each(%hash) ) {
-
# $key = $field[1];
-
# if ($key != $field[0] || $key = $field[1]) {
-
# print "$key => $value\n";
-
# }
-
# }
-
-
# foreach my $key (keys %hash) {
-
# print "$key = $hash{$key};
-
# }
-
-
# my $feature;
-
# my @test_array = <IN>;
-
# print "$field[1]\n";
-
# foreach $value (@created_array) {
-
# my $value = $hash{@created_array};
-
# print $value;
-
# }
-
-
# print "$field[0][0]\n";
-
# foreach my $key (keys %hash) {
-
# print "$key = $hash{$key}\n";
-
# }
-
-
# foreach ($
-
# print "$#field";
-
# test_array = split(",");
-
-
# foreach my $key (keys %hash) {
-
# $key = $field[1];
-
# print "$key = $hash{$key}\n";
-
# }
-
# foreach ($field[1]) {
-
# print OUT "$field[1]", ",";
-
# }
-
-
# if ($field[1] > 0) {
-
# my $field1 = $compare{$field[1]};
-
# print "$field1\n";
-
# }
-
# print "$field[1]";
-
-
# my @field1 = $compare{$field[1]};
-
# print "@field1";
-
# print "$field[1]";
-
}
-
close (INFILE);
-
Thanks
Sorry about the mess heres a better idea of what im trying to do : -
#!/usr/bin/perl
-
#use warnings;
-
-
open(INFILE,"perl_data.csv") || die ("Could not open file");
-
open(OUT, ">test.csv") || die("Could not write file");
-
open(IN, "test.csv") || die("Could not open file");
-
-
my @field =(0);
-
my $field;
-
my @column1=(0);
-
while (<INFILE>) {
-
s/\n//;
-
-
@field =split(",");
-
my %hash = ($field[0],$field[3]);
-
while ((my $key, my $value) = each(%hash)) {
-
if($field[1] != $field[0]) {
-
$key = $field[1];
-
print "$field[0]","$field[1]","field[3]", "$hash{$key}\n"
-
} else {
-
print "$field[0]","$field[1]","field[3]", "field[3]\n"
-
}
-
}
-
when I do this though only the data that match in column one and column two work..
KevinADC 4,059
Recognized Expert Specialist
You may need to follow this up with a tutorial in regards to references. - use warnings;
-
use strict;
-
#use Data::Dumper;
-
my %hash;
-
open(IN,"data.csv") || die ("Could not open file");
-
while(<IN>){
-
chomp;
-
my @t = split(/,/);
-
$hash{$t[0]} = { parent => $t[2], child => $t[1] };
-
}
-
close(IN);
-
#print Dumper \%hash;
-
foreach my $key (sort {$a <=> $b} keys %hash) {
-
print join(',', $key,
-
$hash{$key}{child},
-
$hash{$key}{parent},
-
$hash{$hash{$key}{child}}{parent} );
-
print "\n";
-
}
I used "parent" and "child" really for my own benefit in figuring this out. No implication of a parent/child relationship is being implied, although there is similarity to a parent/child tree structure. Hopefully your real data is also applicable to this type of data structure.
The first three tutorials on this page discuss references: http://perldoc.perl.org/index-tutorials.html
Thanks for the advice,I almost have the program working Now the problem is when I print the line I get the wrong output heres the code thus far: -
open(INFILE,"perl_data.csv") || die ("Could not open file");
-
open(OUT, ">test.csv") || die("Could not write file");
-
open(IN, "test.csv") || die("Could not open file");
-
-
$counter =1;
-
while(<INFILE>) {
-
chomp;
-
(@data1[$counter],@data2[$counter],@data3[$counter],@data4[$counter])= split(/,/);
-
$counter++;
-
}
-
close(INFILE);
-
-
for (my $i = 1; $i <= $#data1; $i++) {
-
$n = @data2[$i];
-
if(@data3[$i] >= .5) {
-
print @data4[$n],@data4[$i],"\n";
-
#print $data4[$i];#," ",@data4[$i]," ",@data1[$i],@data2[$n], "\n";
-
}
-
}
-
it wont let have @data4[$n] and @data[$i] on the same line???
I want it to print
boat cat ... but its not letting me print boat if i want cat also on the same line????
KevinADC 4,059
Recognized Expert Specialist
can't help without seeing the data you are using in your latest code.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: J. Campbell |
last post by:
OK...I'm in the process of learning C++. In my old (non-portable)
programming days, I made use of binary files a lot...not worrying
about endian issues. I'm starting to understand why C++ makes...
|
by: Roland Hall |
last post by:
I'm looking for information on working with large data files using FSO, XML.
I have a program which creates a large CSV file, over 7mb. It's a rate
table of freight shipping costs.
There are...
|
by: Dennis C. Drumm |
last post by:
I would like to be able to update an xml file located on my hosted server
from my local computer.
The server requires a user name and password to access the web site for
writing or updating...
|
by: Sindarian |
last post by:
This just seems like the most basic thing, but I can't find a simple
description of this process anywhere and in here, everone is talking about
going the other way :( I had .NET create an XML...
|
by: PFI |
last post by:
Hi,
I recently installed SQL Server 2005 Enterprise Edition (9.0.1399) and
I have problems on queries concerning system file manipulation...
For example, when I try to increase the Log File of...
| |
by: kutty |
last post by:
Hi All,
I am loading data to a child table from a text file. the text files
also contains data not referenced by parent key. while loading the data
if one row fails to satisfies the constraint...
|
by: SAL |
last post by:
Hello,
Is it possible to read a CSV from the Client, and bind my Datagrid to the
data in the CSV file without uploading the file to the Server first?
I have tried and in Debug mode on my...
|
by: rdemyan via AccessMonster.com |
last post by:
Anyone have any ideas on how to determine when the back-end file (containing
only tables) has been updated with new data.
The date/time of the file won't work because it gets updated to the...
|
by: for.fun |
last post by:
Hi all,
I have a really short time to perform a C++ development task and I
need to :
- find some string in a file
- move some text data from one place of the file to another
- replace some...
|
by: pakerly |
last post by:
How would i do this, convert a test file to excel?
Lets say my text file has fields like this:
NUMBER NAME ADDRESS PHONE
11002 Test1 ...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |