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: Beginner:
How to append or prepend a string to a variable

 

First page Previous page 1 2 Next page Last page  View All


Tejas
User

Nov 19, 2014, 6:41 AM

Post #26 of 46 (9973 views)
Re: [Zhris] How to append or prepend a string to a variable [In reply to] Can't Post

error at split

Quote
Use of uninitialized value in regexp compilation at ./Test_Stand.pl line 48, <$handle> line 2419.
Use of uninitialized value in regexp compilation at ./Test_Stand.pl line 48, <$handle> line 2420.


Thanks


Zhris
Enthusiast

Nov 19, 2014, 6:46 AM

Post #27 of 46 (9971 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

What are the lines indicated by the errors at lines 2419 and 2420 of the input file it falls over on?


(This post was edited by Zhris on Nov 19, 2014, 6:47 AM)


Tejas
User

Nov 19, 2014, 6:48 AM

Post #28 of 46 (9967 views)
Re: [Zhris] How to append or prepend a string to a variable [In reply to] Can't Post

when i tried to print the delimiter , its printing space
Actually the input file is ok
And for all the lines its throing the same error
I just pasted a part of it

print " $delim \n";



here are the input file's data , treat all the lines as different,all of them have to be printed


Quote
gl_file.txt

04|2110|0|Failure|TBD
04|2110|0| Settlement|TBD
04|2110|0| Failure|TBD
04|2110|0|Failure|TBD
04|2110|0|Failure|TBD
04|2110|0|Settlement|TBD
04|2110|0|Failed|TBD
04|2110|0|Settlement|TBD
04|2110|0|lure|TBD
04|2110|0|TBD


sl_file.txt

1;04;1;Sale
1;2R;1;Sale
1;04;1;Sale
1;2R;1;Sale
1;04;3;Refund
1;04;4;Refund



(This post was edited by Tejas on Nov 19, 2014, 6:53 AM)


Zhris
Enthusiast

Nov 19, 2014, 6:52 AM

Post #29 of 46 (9960 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

I have modified the code again to remove the use of qr, in case its not compatible with your Perl version, can you try again.


Tejas
User

Nov 19, 2014, 7:01 AM

Post #30 of 46 (9957 views)
Post deleted by Tejas [In reply to]

 


Zhris
Enthusiast

Nov 19, 2014, 7:18 AM

Post #31 of 46 (9953 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

Ah, so removing use of the regular expression qr quote operator worked. What version of Perl are you using out of interest.

If the condition is working fine, then you can leave it, but what if org doesn't match either '04', '2Y', '2R', '3S', '79', '3T', '3M' or '2H', how do you handle. Note it will also test this condition for all input files, you might need to chuck in $worksheet_id eq 'SL'. My preference would be to extend the input hash, perhaps add a handler / closure which orgs are run through.


Quote
Can i change this to ARGV[0] and ARGV [1]
I might have more input files to , now they are just 2


You can replace the hardcoded filenames with @ARGV values i.e. path => $ARGV[0],, but if you you add more input files, you would have to extend the %input hash appropriately. If you want to build the %input hash dynamically, then you will have to supply all necessary info via args i.e. --GL.path=..., --GL.delim=..., --SL.path=..., --SL.delim=..., then parse them.

Finally, if you provide your final code once you have finished, I can take a look at it, re-look through the Excel::Writer::XLSX documentation and see if I can tidy / improve it as best as possible. My latest version was rushed.

Chris


(This post was edited by Zhris on Nov 19, 2014, 7:22 AM)


Tejas
User

Nov 19, 2014, 7:23 AM

Post #32 of 46 (9945 views)
Post deleted by Tejas [In reply to]

 


Zhris
Enthusiast

Nov 19, 2014, 7:33 AM

Post #33 of 46 (9937 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

I have edited my latest code again to support mapping of orgs. I used a hash lookup approach, but if the hash got too large, I may take a different approach.

Also, once we have a finalised version I will comment it for you.

Chris


(This post was edited by Zhris on Nov 19, 2014, 7:34 AM)


Tejas
User

Nov 19, 2014, 7:36 AM

Post #34 of 46 (9934 views)
Re: [Zhris] How to append or prepend a string to a variable [In reply to] Can't Post

Thanks a lot for your support.
i will test it now.
Please explain the code line by line too


Because i see that tab doesnt get created if the data is nt available for a speific file for a specific org, which is great

For example If us has GL data and no sl data, only gl tab is getting created.
By Looking at the code , its termendously generic.

Please explain in line by line


Thanks


Zhris
Enthusiast

Nov 19, 2014, 7:41 AM

Post #35 of 46 (9933 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post


Quote
Because i see that tab doesnt get created if the data is nt available for a speific file for a specific org, which is great

For example If us has GL data and no sl data, only gl tab is getting created.


Yup, I made the assumption that this would be desirable, glad it worked out. It wouldn't be difficult to adjust if you ever needed to create both tabs regardless of whether or not they have data.

Once you are happy with the code, please post the version you are using, just in case there are differences, and I will comment it for you.

Chris


(This post was edited by Zhris on Nov 19, 2014, 7:42 AM)


FishMonger
Veteran / Moderator

Nov 19, 2014, 7:47 AM

Post #36 of 46 (9929 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

Chris has done a good job with the xls file creation solution so I'll just point out 1 or 2 things that will help you to write more readable/maintainable code.

When writing for/foreach loops don't use the C style construct. The Perl construct is cleaner and more efficient in its execution. Chris has already given examples using the Perl construct so no need for me to duplicate that example.

Your alternation method for testing the value of $org is overly verbose and doesn't scale well. A better approach would be to put those values into an array and use either the any or first function from the List::Util module to handle the test.
http://search.cpan.org/~pevans/Scalar-List-Utils-1.41/lib/List/Util.pm


Code
my @org_values = qw(04 2Y 2R 3S 79 3T 3M 2H); 

while ( my $line = <$handle> ) {
chomp $line;

my ( $org, @data ) = split /\Q$delim\E/, $line;
$org = 1 if any {$_ eq $org} @org_values;


Another approach would be to build a hash using those values as the keys and use the exists function to do a simple hash lookup. Chris alluded to this in one of the posts.


Code
my %orgs = map {$_ => 1} qw(04 2Y 2R 3S 79 3T 3M 2H); 

while ( my $line = <$handle> ) {
chomp $line;

my ( $org, @data ) = split /\Q$delim\E/, $line;
$org = 1 if exists $orgs{$org};



Tejas
User

Nov 19, 2014, 7:54 AM

Post #37 of 46 (9926 views)
Re: [FishMonger] How to append or prepend a string to a variable [In reply to] Can't Post

my %orgs = map {$_ => 1} qw(04 2Y 2R 3S 79 3T 3M 2H);


how to have multiple orgs here

Code
my %input = 
( GL =>
{
path => $ARGV[0],
delim => '|',
org_map =>
{
'3H' => '1',
'04' => '1',
'2R' => '1',
'2Y' => '1',
'79' => '1',
'3T' => '1',
'3M' => '1',
'79' => '1' ,
'4R' => '3' , #gb
44 => '5' #de
}
},
SL =>
{
path => $ARGV[1],
delim => ';',
org_map => undef,
}
);


Also can u explain what map does
I have always been doing things in C style, as i dont have a great knowlege on this


Thanks
Tejas


(This post was edited by Tejas on Nov 19, 2014, 7:57 AM)


FishMonger
Veteran / Moderator

Nov 19, 2014, 8:08 AM

Post #38 of 46 (9919 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

If you want separate org_map definitions for GL and SL, then yes, you can define them like that inside your %input hash and the test could be:

Code
$org = $input{GL}{org_map}{$org} if exists $input{GL}{org_map}{$org};



Quote
Also can u explain what map does

See perldoc -f map
http://perldoc.perl.org/functions/map.html


(This post was edited by FishMonger on Nov 19, 2014, 8:12 AM)


Tejas
User

Nov 19, 2014, 8:09 AM

Post #39 of 46 (9918 views)
Re: [Zhris] How to append or prepend a string to a variable [In reply to] Can't Post

Hi Chris

Quote
1. U have made a great effort to accomplish the imposible task
for me any given day
2. My problem statements and explanations were vague and u have understood them too. :)

Iam attachoing the code.
I ll be glad if you can comment every line.
So thai
i will get knowledge on this particular thing and might be helpful in my future projects.
Attachde :)
Thanks

Tejas


(This post was edited by Tejas on Nov 19, 2014, 8:28 AM)
Attachments: Code.pl (2.61 KB)


Zhris
Enthusiast

Nov 19, 2014, 8:26 AM

Post #40 of 46 (9908 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

Hi,

No problem at all, it can often be difficult to describe the problem you have at hand, if you supply example input and example output that covers every scenario from the beginning, it is alot easier to decipher what needs to be done to get from input to output.

Also, I think you have forgotten to attach your code.

Chris


Tejas
User

Nov 19, 2014, 8:47 AM

Post #41 of 46 (9899 views)
Re: [Zhris] How to append or prepend a string to a variable [In reply to] Can't Post

There is a small code change i have made

Removed $workbook from while and declared above while
Attachments: Test_Stand.pl (3.49 KB)


Zhris
Enthusiast

Nov 19, 2014, 9:26 AM

Post #42 of 46 (9895 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

Hi,

I have commented the first version you attached. It was actually quite difficult to explain, particularly aspects surrounding the state hash, I have rushed it therefore please raise anything you still struggle to understand. Usually I don't comment my own code, since I'm the only one who ever looks at it, and I break it up into well defined / namespaced functions that are often self explanatory. I definately need more practice in explaining code to others.

If you make changes to the code, please feel free to post the updated version, as promised I will go through at some point and re-write properly.

Chris
Attachments: explained.pl (5.34 KB)


Tejas
User

Nov 19, 2014, 9:48 PM

Post #43 of 46 (9880 views)
Re: [Zhris] How to append or prepend a string to a variable [In reply to] Can't Post

Thanks Zhris

Next step has been
Now i have to create a third tab (We wont be having an input file for this)

Can u tell me how to create a tab even thgough we do not have data.
Now we are just creating a tab dependent on the input data,
But i need to have a empty tab if data is nt there.
So , i can take a cue form this and create a third tab for comparing.

And i will let you know what has to be done in that tab.

As of now, i can tell you that i will have to
http://perlguru.com/gforum.cgi?do=post_view_flat;post=80220;page=2;sb=post_latest_reply;so=ASC;mh=25;

Create retail, digital and various hashes from the two inputs and calculate the amounts and paste it in third tab.
You can see that in url above


Thanks
Tejas


Zhris
Enthusiast

Nov 20, 2014, 3:49 AM

Post #44 of 46 (9873 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

Hi,


Quote
There is a small code change i have made
Removed $workbook from while and declared above while


Moving the declaration is not a problem, but you later call $workbook->close, the problem with this is that you will only close the last workbook object you dealt with. You can access any of the workbook objects via the workbooks state hash, to close all workbooks:


Code
$workbooks{$_}{workbook}->close for ( keys %workbooks );



Quote
Now i have to create a third tab (We wont be having an input file for this)
Can u tell me how to create a tab even thgough we do not have data.
Now we are just creating a tab dependent on the input data,
But i need to have a empty tab if data is nt there.
So , i can take a cue form this and create a third tab for comparing.


The easiest way would be to create it right after you have instantiated the Excel::Writer::XLSX object then add it to the workbooks state hash if necessary:


Code
$workbook = Excel::Writer::XLSX->new( "${org}_GL_SL.xlsx" ); 
my $compare_worksheet = $workbook->add_worksheet( 'compare_Sheet' );
$workbooks{$org}{worksheets}{compare}{worksheet} = $compare_worksheet;



Regards,

Chris


(This post was edited by Zhris on Nov 20, 2014, 4:46 AM)


Zhris
Enthusiast

Nov 20, 2014, 4:44 AM

Post #45 of 46 (9864 views)
Re: [Tejas] How to append or prepend a string to a variable [In reply to] Can't Post

Hi,

After reading through the documentation for Excel::Writer::XLSX in more detail, I believe that we have taken the best approach, by storing the state of each workbook inside a hash. As mentioned previously, the module unfortunately doesn't provide a nice interface to manage multiple workbooks, fetch the format from the workbook, manage multiple worksheets within a workbook ( although it could be done via the sheets methods, its easier to deal with sheets by id ), fetch the last row number from a worksheet etc. If I were to rewrite, there would be little difference other than improved namespacing, validation where necessary, and breaking the code up into functions with more specific roles.

We discussed building input dynamically from args, therefore removing the hardcoded input hash. You could write your own input function to parse the args in whatever manner you wish. With regards to mapping orgs, it would be easiest to supply a boolean value which indicates whether orgs should be mapped or not, then hardcode a single mapping hash. A rough example:

$perl input.pl GL.path='gl.txt' GL.delim='|' GL.map_orgs='1' SL.path='sl.txt' SL.delim=';'

Code
use Data::Dumper; 

my $input = input( \@ARGV );

print Dumper $input;

sub input
{
my ( $args ) = @_;

my $input = { };

for my $arg ( @$args )
{
my ( $key, $val ) = split /=/, $arg;
my ( $id, $attr ) = split /\./, $key;

$input->{uc $id}->{lc $attr} = $val;
}

return $input;
}


Code
$VAR1 = { 
'GL' => {
'map_orgs' => '1',
'path' => 'gl.txt',
'delim' => '|'
},
'SL' => {
'path' => 'sl.txt',
'delim' => ';'
}
};


Regards,

Chris


(This post was edited by Zhris on Nov 20, 2014, 5:32 AM)


Tejas
User

Nov 20, 2014, 5:25 AM

Post #46 of 46 (9860 views)
Re: [Zhris] How to append or prepend a string to a variable [In reply to] Can't Post

Thanks Chris.
I am going ahead with th eprev approach, as i have to send numerous arguements

The next phase os quite tough to deal with
And , i will explain you about that, as explaining it is again hard task .


Thanks
Tejas

First page Previous page 1 2 Next page Last page  View All
 
 


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

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