CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Perl code to get a logic

 



venu
New User

Oct 29, 2014, 3:20 AM

Post #1 of 8 (16824 views)
Perl code to get a logic Can't Post

Hi all,

I have two files. By using these two files I need to generate a file.

File1 will contain:

R1 7n 1n
R2 8n 1n
R3 1n 2n
R4 2n 3n
R5 3n 4n
R6 3n 5n
R7 3n 6n

File2 will contain

7n 5
8n 5
4n -2
5n -4
6n -4

I need output file:

R1 5
R2 5
R3 10
R4 10
R5 2
R6 4
R7 4


Please see attached screenshot

Thank you


BillKSmith
Veteran

Oct 29, 2014, 6:47 AM

Post #2 of 8 (16812 views)
Re: [venu] Perl code to get a logic [In reply to] Can't Post

If you are looking for someone to do your project for you, you have come to the wrong place.

If you really want help, show us where you are stuck. Do you need help with the algorithm? The I/O? The regular expressions? You are posting in the Intermediate Forum. We expect you to have a general knowledge of all these subjects.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Oct 29, 2014, 3:10 PM

Post #3 of 8 (16806 views)
Re: [venu] Perl code to get a logic [In reply to] Can't Post

As said in another form by Bill, this forum is not a code writing service. We're very much willing to help you, but we're not going to do your work (or your homework assignment) for you. So if you presented some code (even broken) that you have tried, we would try to help you correcting it. But you did not so far.

But we can still try to give you some clues on how to do it. This is the way I would probably solve your problem (at least the way I understood it initially):

- First open and read the second file line by line, split each line on spaces and populate a hash with the first field (e.g. 7n) as the hash key and the second field as the hash value. You can then close the second file, which is no longer needed.

- Then open the first file and read it line by line, split the lines on spaces, and use the second field to lookup into the hash, and finally print the first field of the first file and the value recovered from the hash.

This is really easy.

However, the logic above works for the two first lines of input of your first file, but I cannot figure out how the "R3 1n 2n" line can give you 10. For some other lines, it seems that if the second field is not found, then you use the third field and negate the corresponding value coming from the second file.

So besides showing some coding attempt, you'll also have to explain how you plan to derive your desired output from the two files for lines 3 and 4 (for example) of your first unput file. At this point, I can't make sense of your output with the input you showed.


venu
New User

Oct 30, 2014, 4:08 AM

Post #4 of 8 (16791 views)
Re: [Laurent_R] Perl code to get a logic [In reply to] Can't Post

Thanks Laurent and Bill,

I am from circuit design domain. So I don't know much perl. And Sorry for incomplete question. I will follow Laurent suggestion and I will try at my end and let you know if I struck anywhere.

Previously I failed to attach screenshot. Now I attached it. Just share If any logic strikes to you. I will write code by my self by following your suggestions or logic.

In file1 :

R1 7n 1n
R2 8n 1n
R3 1n 2n
R4 2n 3n
R5 3n 4n
R6 3n 5n
R7 3n 6n

File2 will contain currents of Resistors(I modified file2 in this post)

R1 5

R2 5

R5 -2

R6 -4

R7 -4

Need Output:

R1 5

R2 5

R3 10

R4 10

R5 -2

R6 -4

R7 -4

Please see below image. It is combination of first two files and output. Green colour one is my output. File1 will discribe R device connections and File2 will give currents in all ends.







Main logic is ::::::: Currents in all resistors branches are some of entering currents and leaving currents at any resistor terminal. I want output like this

R1 - 5 ( branch current we will directly get from file2 )

R2 - 5 ( from file 2 )

R3 - 5+5=10 because 1n is connected to R1 and R2

R4 - 10 because it is connected to R3

R5 - -2 directly get from file2

R6 - -4 directly get from file2

R7 - -4 directly get from file2



Thank you,

Venu


(This post was edited by venu on Oct 30, 2014, 5:26 AM)


BillKSmith
Veteran

Oct 30, 2014, 7:06 AM

Post #5 of 8 (16724 views)
Re: [venu] Perl code to get a logic [In reply to] Can't Post

Your "solution" for load R4 assumes that you have already done R3. If you do the loads in the order that they appear in file 1 that will work in this case. Note that if the order of the specifications for R3 and R4 were reversed in file 1 you would still have exactly the same network, but you could not solve it. A recursive algorithm would solve that problem, but still may not be sufficiently general. We need to know about the class of problems you must be able to solve.


Also note that this example is over specified. In theory, you could still solve it if any one of the currents in file 2 were omitted. In this case, they are consistent. It is important that your program check for that.

BTW, how did you post the graphic. That is neatSmile.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Oct 30, 2014, 12:54 PM

Post #6 of 8 (16627 views)
Re: [BillKSmith] Perl code to get a logic [In reply to] Can't Post

OK, now I understand what you are tying to do and what your files really means. You'll have to admit that without the context (i.e. that the "1n" or "2n" meant nodes in an electrical circuit), the data did not make any sense.


venu
New User

Oct 31, 2014, 3:42 AM

Post #7 of 8 (16599 views)
Re: [Laurent_R] Perl code to get a logic [In reply to] Can't Post

Hi Bill and Laurent,

By following previous posts I am able to write for this code upto some level. But I strucked at a location. See my inputs , outputs and code. Please correct it.


Test file:

R1 7n 1n
R2 8n 1n
R3 1n 2n
R4 2n 3n
R5 3n 4n
R6 3n 5n
R7 3n 6n


Ref file:

R1 5
R2 5
R5 -2
R6 -4
R7 -4


I am getting OutPut like below:

R1 5
R2 5
R5 -2
R6 -4
R7 -4
R1 0
R1 0


But expected Output is

R1 5
R2 5
R5 -2
R6 -4
R7 -4
R3 10
R4 10

I have used recursive function as Bill suggested.



#!/usr/bin/perl

my $slurp;
{
local $/ = undef;
open my $textfile, '<', 'ref' or die $!;
$W = <$textfile>;
close $textfile;
}

my $slurp;
{
local $/ = undef;
open my $textfile, '<', 'test' or die $!;
$M = <$textfile>;
close $textfile;
}


@list = split (/\n/, $M);

#print $W;

foreach $name (@list) {
@list1 = split (/\s+/, $name);

if( $W !~/$list1[0]/) {

$R=$name;
$sum=0;
recursive_net_check();
}
}

print $W;

sub recursive_net_check {

my $index = 0;
$index++ until $list[$index] eq $R;
splice(@list, $index, 1);

@list1 = split (/\s+/, $R);

foreach $name (@list) {

if(( $name=~/$list1[1]/) || ( $name=~/$list1[2]/)) {

if ($W !~/$list1[0]/) {
$R=$name;
recursive_net_check();
}
else{
@listW = split (/\n/, $W);
foreach $w1 (@listW) {
@listW2 = split (/\n/, $w1);
if($listW2[0]=~/list1[0]/){
$sum=$sum+$listW2[1];
print $sum;
}
}
}
}
}

$W= join '', $W, $list1[0], " ", $sum , "\n";
}



Thank you,

Venu.


(This post was edited by venu on Oct 31, 2014, 3:44 AM)


BillKSmith
Veteran

Oct 31, 2014, 7:07 AM

Post #8 of 8 (16590 views)
Re: [venu] Perl code to get a logic [In reply to] Can't Post

The code that you posted is not the code that you ran. The input file names are undefined and there is no code that could print the output that you show.

When you want help, it is very important to post code that demonstrates the problem! It is also a good idea to make things as easy as possible for your readers.

Always use strict and warnings. Declare all variables in the smallest possible scope. Fix all the problems that perl reports. This will allow you to fix many problems yourself. Your readers can concentrate on the harder problems.

Indent you code consistently and preserve that indenting with code tags.

Use meaningful variable names. I cannot tell which is which with your names @list and @list1. Use something like @network and @currents.

Your subroutine has no explicit arguments or returns. Global data is seldom a good idea especially with recursion.
Your call would be much clearer if your main program could do:

Code
my $current = get_current_for_load($R); 
print $R, $current;

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives