473,748 Members | 8,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Processing a Data Structure

7 New Member
Hi,

I have a question about converting some of the data in my dataset but leave some data the way it is.

I have a hash and if the key is present and the Value of the Hash equals B it should convert these number but if B is not present the data should remain the same.

my output is that all the data is converted not in the only the cases were B is present.

This is the script i have

Expand|Select|Wrap|Line Numbers
  1. while ($data = <Dataset>) {
  2.     if (exists $hash[1]) {
  3.         if ($hash[2]==B) {
  4.             for ($i = 0; $i < length($data); $i++) {
  5.                 $characterString = substr($data, $i, 1);
  6.                 $newCode = $characterString;
  7.                 if ($characterString eq "1") {
  8.                     $newCode = "0";
  9.                 }
  10.                 if ($characterString eq "0") {
  11.                     $newCode = "1";
  12.                 }
  13.                 if ($characterString eq "2") {
  14.                     $newCode = "3";
  15.                 }
  16.                 if ($characterString eq "3") {
  17.                     $newCode = "2";
  18.                 }
  19.                 substr($data, $i, 1) = $newCode;
  20.             }
  21.             print "$data\n";
  22.         }
  23.         else {
  24.             print $data\n";
  25.         }
  26.  
I hope someone can help me with this.
Feb 28 '07 #1
13 1795
KevinADC
4,059 Recognized Expert Specialist
Do you have an array or a hash?

Expand|Select|Wrap|Line Numbers
  1. if (exists $hash[1]) {
$hash[1] is an array index, not a hash key. Hash keys use curly brackets {}

Expand|Select|Wrap|Line Numbers
  1. if (exists $hash{1}) {
Feb 28 '07 #2
KevinADC
4,059 Recognized Expert Specialist
'B' is a string (most likely) not a number, so use 'eq' instead of '==':

Expand|Select|Wrap|Line Numbers
  1. $hash[2] eq 'B'
Feb 28 '07 #3
Iris83
7 New Member
Thank you for your help. I tried it like you said it is a hash so this is what i used but im not getting any output! I hope you can help me figure out what i should do!

Expand|Select|Wrap|Line Numbers
  1. while ($data = <Dataset>) {
  2.     if (exists $hash{1]} {
  3.         if ($hash{2} eq 'B') {
  4.             for ($i = 0; $i < length($data); $i++) {
  5.                 $characterString = substr($data, $i, 1);
  6.                 $newCode = $characterString;
  7.                 if ($characterString eq "1") {
  8.                     $newCode = "0";
  9.                 }
  10.                 if ($characterString eq "0") {
  11.                     $newCode = "1";
  12.                 }
  13.                 if ($characterString eq "2") {
  14.                     $newCode = "3";
  15.                 }
  16.                 if ($characterString eq "3") {
  17.                     $newCode = "2";
  18.                 }
  19.                 substr($data, $i, 1) = $newCode;
  20.             }
  21.             print "$data\n";
  22.         }
  23.         else {
  24.             print $data\n";
  25.         } 
  26.  
  27.  
Mar 1 '07 #4
Iris83
7 New Member
Hi i want to use the keys and values of a hash to compare them with other files. But i can't acces them.

This is my code i hope somone can help!
Expand|Select|Wrap|Line Numbers
  1.  
  2. my %hash;
  3.  
  4. while( <Data> ) {
  5.     @elements = split /,/, $_;
  6.  
  7.     if( $elements[1] ne '' ) {
  8.         $hash{ $elements[1] } = $elements[2];
  9.     }
  10. }
  11.  
  12. while ($data = <Dataset>) {
  13.     if (exists $hash{1}) {
  14.         if ($hash{2} eq 'B')
  15.             print $data;
  16.         else {
  17.             print $data;
  18.         }
  19.     }
  20. }
  21.  
Mar 1 '07 #5
docsnyder
88 New Member
@Iris83

I'm not quite sure what you are intending to do.

Expand|Select|Wrap|Line Numbers
  1. $hash{ $elements[1] } = $elements[2];
Do you really mean the second and the third element? Remember, the first array element has index 0.

You are accessing $hash{1} and $hash{2}. Are 1 and 2 really the hash keys you mean?

Expand|Select|Wrap|Line Numbers
  1. if ($hash{2} eq 'B')
  2. print $data;
  3. else {print $data;
  4. }
Why an if-statement when printing data in any case?

Please, can you give a short example of what your input files consist of?

Greetz, Doc
Mar 1 '07 #6
KevinADC
4,059 Recognized Expert Specialist
check you code for proper syntax:

{1]

should be:

{1}
Mar 1 '07 #7
Iris83
7 New Member
The syntax is indeed {1} in my script but it doesn't work!

so any tips would be appreciated!
Mar 1 '07 #8
KevinADC
4,059 Recognized Expert Specialist
need to see some of the data you are working with.
Mar 1 '07 #9
Iris83
7 New Member
I have 2 datasets one containing data like this

11,1,B
12,2,B
13,3,T
21,4,T
22,5,T

From this data i want the 2nd row to be the keys in the hash and the 3th row the values.

my 2nd data set looks like this

1 1 1 0 0
2 1 0 1 1
3 1 0 1 1
5 1 0 0 0

for example if the first key which is 1 exists and the value is B i want to reverse the data from 1 to 0 and 0 in 1. If the key is present and the value is T it should just print the normal data.

I was told that you could acces the hash keys like this $hash{1} and the values like this $hash{2} .

I hope this clarifies thing and you are able to help me with my problem.

Expand|Select|Wrap|Line Numbers
  1. my %hash;
  2.  
  3. while( <Data> ) {
  4.     @elements = split /,/, $_;
  5.  
  6.     if( $elements[1] ne '' ) {
  7.         $hash{ $elements[1] } = $elements[2];
  8.     }
  9. }
  10.  
  11. while ($data = <Dataset>) {
  12.     if (exists $hash{1}) {
  13.         if ($hash{2} eq 'B') {
  14.  
  15. for ($i = 0; $i < length($data); $i++) {
  16.                 $characterString = substr($data, $i, 1);
  17.                 $newCode = $characterString;
  18.                 if ($characterString eq "0") {
  19.                         $newCode = "1";
  20.                 }
  21.                 if ($characterString eq "1") {
  22.                         $newCode = "0";
  23.                         }
  24.  
  25.                 substr($data, $i, 1) = $newCode;
  26.             print $data;
  27.         else {
  28.             print $data;
  29.         }
  30.     }
  31. }
  32. }
  33. }
Mar 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1578
by: Anders Søndergaard | last post by:
Hi, I'm trying to process a large filesystem (+20 million files) and keep the directories along with summarized information about the files (sizes, modification times, newest file and the like) in an instance hierarchy in memory. I read the information from a Berkeley Database. I'm keeping it in a Left-Child-Right-Sibling instance structure, that I operate on recursively.
9
12318
by: Itay | last post by:
Hi all , Suppose i have structurs defines as follows : struct S1 { int n1 ; int n2 ; int n3 ; };
7
7005
by: gj | last post by:
I have an application in Access 97 I will be rewriting in the latest version of Access in 6 months. In the meantime, does anyone know of an ActiveX control I can add into an Access 97 form to allow the validation of Credit Card numbers and also processing of payments. Ta very muchly. GJ
3
2279
by: mrhicks | last post by:
Hello all, I have a question regarding efficeny and how to find the best approach when trying to find flag with in a structure of bit fields. I have several structures which look similar to typedef unsigned long int uint32; /* 32 bits */ // Up to 36 request flags, so this will take up to 3
1
301
by: Maury Markowitz | last post by:
Thanks to a tremendous amount of patient help from Rob over in the interop group I have managed to successfully wrap a vendor-provided DLL and call it successfully from managed C# code. Now I'd like to process the data returned, a void* buffer which I have "cast" into a byte on the C# side. The code, like many C programs, uses casting on top of the data buffer to impose one or more typedef'ed structs onto the void*. For instance the first...
3
7666
by: ecov | last post by:
Is there any easy way to read into a dataset a file that was created from a BCP command. The file is in a fixed length format. I would also like to be able to write out a dataset to the same type of file structure. The obvious solution would be to use XML file processing however that is not an option as I have a third party vendor that is not capable of processing XML files.
10
1801
by: _(d)IEGO | last post by:
Hello everyone. I need help regarding the following: Given the following table: CREATE TABLE T1 (C1 nvarchar(10), C2 money) INSERT INTO T1 VALUES ('A',1) INSERT INTO T1 VALUES ('B',2) INSERT INTO T1 VALUES ('C',3) let's say that i have this table in a local server and i want to upload
6
3011
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation for one of the Pear modules for creating XML and it didn't look like much. I've used Perl XML:Twig and I have the impression that there is more Perl stuff available as well as the Perl stuff being well documented as I have a Perl DBI book, a Perl...
2
2278
by: vectorizor | last post by:
Hi all, I am writing an image processing algorithm that goes across an image first row-wise and then column-wise. For illustration purposes, imagine that for every pixels, the output is computed as the sum of the input and its previous neighbourg. In the horizontal pass (i.e. row-wise), the previous neighbourg is defined as the pixel on the left. This is followed by a vertical pass, whereby the previous neighbourg is defined as the...
0
1866
by: dataentryoffshore | last post by:
Get a Discount up to 60% on data entry, data capture, dataentry services, large volume data processing and data conversion services through offshore facilities in India. Offshore data entry also provides form data entry, data capture, HTML/SGML coding, image scanning, file conversion with low cost, high quality,99.98% accuracy and time bound. Data Conversion Services offer cost effective data conversion projects, Outsourcing Data...
0
9370
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.