Gossamer Forum
Home : General : Perl Programming :

Interesting Concatenation Problem - Require Help

Quote Reply
Interesting Concatenation Problem - Require Help
HI All ,

I am facing an interesting problem

Please help me resolve the same.

I have two functions. The main function reads the machine name from an input config file , passes it on to sub_main() function .
In the sub_main() function , I need to concatenate this machine name with ".world" .

However the concatenated string is wierd. However both the inpus to the concatenation are OK , the result is not

Please Help

Attaching code for your reference.



#!/usr/bin/perl

main();

sub main () {

my %config_hash=();
my @machines=();
my $i=0;
open(CONFIG, "<config_details.cfg") || die "I/O Success - Cannot process get_max_ri.sql file";

while (<CONFIG>)
{
my @config_line=split(/=/,$_);
print "$config_line[0]\n";
$config_hash{$config_line[0]}=$config_line[1];
}

@machines=split(/,/,$config_hash{"MACHINE NAMES"});
my $NUM_MACHINES=@machines;

for ($i=0;$i<$NUM_SMF_MACHINES;$i++)
{
chomp($machines[$i]);
sub_main($i+1,$machines[$i]);
}

}


sub sub_main()

{
my $mac2=shift();
my $mac3=$mac2.'.world';
print $mac3;
print "\n";

}
Quote Reply
Re: [gpta_varun] Interesting Concatenation Problem - Require Help In reply to
Slight Change in the code

#!/usr/bin/perl

main();

sub main () {

my %config_hash=();
my @machines=();
my $i=0;
open(CONFIG, "<config_details.cfg") || die "I/O Success - Cannot process get_max_ri.sql file";

while (<CONFIG>)
{
my @config_line=split(/=/,$_);
print "$config_line[0]\n";
$config_hash{$config_line[0]}=$config_line[1];
}

@machines=split(/,/,$config_hash{"MACHINE NAMES"});
my $NUM_MACHINES=@machines;

for ($i=0;$i<=$NUM_MACHINES;$i++)
{
chomp($machines[$i]);
sub_main($i+1,$machines[$i]);
}

}


sub sub_main()

{
my $A=shift();
my $mac2=shift();
my $mac3=$mac2.'.world';
print $mac3;
print "\n";

}
Quote Reply
Re: [gpta_varun] Interesting Concatenation Problem - Require Help In reply to
So you are trying to add ".world" to the end of the string, or just "world"?

You also dont give us an example of whats being outputted, appart from saying its "weird" Wink

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Interesting Concatenation Problem - Require Help In reply to
 
Want to print "XYZ.world" .
I am just getting ".world" as the output :(


Thanks
Quote Reply
Re: [gpta_varun] Interesting Concatenation Problem - Require Help In reply to
Sounds more like the value isn't being passed in. Do a couple of tests:



Code:
@machines=split(/,/,$config_hash{"MACHINE NAMES"});
my $NUM_MACHINES=@machines;

use Data::Dumper;
print "Dump of matching: " . Dumper(@machines);


for ($i=0;$i<=$NUM_MACHINES;$i++)

..and:

Code:
sub sub_main {
my ($A,$mac2) = @_;

my $mac3=$mac2.'.world';
print $mac3;
print "\n";
}

Basically, check to see if what you are execting to get through, actually is.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Interesting Concatenation Problem - Require Help In reply to
Many Thanks for the reply Andy

I guess , i have got the problem ( not the solution though !)

My config file is
-------------------------
MACHINE NAMES=P1,P2


The result of Data Dump was :
Dump of matching: $VAR1 = 'P1';
$VAR2 = 'P2
';


As a result , while P1.world got printed correctly , P2.world did not!


For removing the new line feed , I tries chomping the array @machines , but it did not solve the problem



--varun
Quote Reply
Re: [gpta_varun] Interesting Concatenation Problem - Require Help In reply to
Before:

my @config_line=split(/=/,$_);

Try doing:

chomp;

This should get rid of the newline bit.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Interesting Concatenation Problem - Require Help In reply to
 
Still no success :(
Quote Reply
Re: [gpta_varun] Interesting Concatenation Problem - Require Help In reply to
What does the code look like now?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Interesting Concatenation Problem - Require Help In reply to
Code:
#!/usr/bin/perl
use Data::Dumper;

main();

sub main () {

my %config_hash=();
my @machines=();
my $i=0;
open(CONFIG, "<config_details.cfg") || die "I/O Success - Cannot process get_max_ri.sql file";

while (<CONFIG>)
{
my @config_line=split(/=/,$_);
chomp;
print "$config_line[0]\n";
$config_hash{$config_line[0]}=$config_line[1];
}


@machines=split(/,/,$config_hash{"MACHINE NAMES"});
my $NUM_MACHINES=@machines;
print "Dump of matching: " . Dumper(@machines). "\n";

for ($i=0;$i<=$NUM_MACHINES;$i++)
{
chomp($machines[$i]);
sub_main($i+1,$machines[$i]);
}

}


sub sub_main()

{
my $A=shift();
my $mac2=shift();

print "$mac2\n";
my $mac3=$mac2.'.world';
print $mac3;
print "\n";

}

Quote:
Output

G> perl test.pl
MACHINE NAMES
Dump of matching: $VAR1 = 'P1';
$VAR2 = 'P2
';

P1
P1.world
P2
.world

.world




Quote Reply
Re: [gpta_varun] Interesting Concatenation Problem - Require Help In reply to
I said *before* that line Tongue

So instead of:

Code:
my @config_line=split(/=/,$_);
chomp;

Try:

Code:
chomp;
my @config_line=split(/=/,$_);

Basically, you need to get rid of the newline bit *before* you split it into the array Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Interesting Concatenation Problem - Require Help In reply to
Quote:

OUTPUT :( :( :( Angelic Mad
G> perl test.pl
MACHINE NAMES
Dump of matching: $VAR1 = 'P1';
';AR2 = 'P2

P1
P1.world
P2
.world

.world
Quote Reply
Re: [gpta_varun] Interesting Concatenation Problem - Require Help In reply to
MM, instead of this:

Code:
chomp;
my @config_line=split(/=/,$_);

Try:

Code:
$_ =~ s/[\r\n]//sg;
my @config_line=split(/=/,$_);

Any difference?

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Interesting Concatenation Problem - Require Help In reply to
Thanks Andy !

Your suggestion worked !

Thanks again !


(Looking forward to bother you again some time later Smile )
Quote Reply
Re: [gpta_varun] Interesting Concatenation Problem - Require Help In reply to
Cool, glad to hear it :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!