473,394 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

concatenation problem

archulu
34
hai this is archulu. i have a problem in my program as for cancatintion the values.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $string = 'N/W_Administrator/Trinee';
  5.  
  6. my $str = 'Level1';
  7. my $len1 = 1;
  8. #my $len1 = 1;    
  9.  
  10. my $a=length($string);
  11.  
  12. if ($a < 15) {
  13.     my $length = 4;
  14.     my $frag = substr $string, 0, $length;
  15.     print "fragment: $frag\n";
  16.  
  17. } else {
  18.     my $lengt = 7;
  19.     my $frag = substr $string, 0, $lengt;
  20.     print "fragment: $frag\n";
  21. }
  22.  
  23. my $frag1 = substr $str, 0, $len1;
  24. my $frag2 = substr $str, 5;
  25. my$sub=$frag."".$frag1."".$frag2;
  26.  
  27. print "frag1: $frag1\n";
  28. print "frag2:$frag2\n";
  29. print "values are:$sub\n";
  30.  
this my program . in ths my progra i want to cancatinate frag(which come from loop as o/p) and frag1 and frag2
but i have a error like this

Global symbol "$frag" requires explicit package name at proj.pl line 28.
Execution of proj.pl aborted due to compilation errors.

i am saved my program as a proj.pl name
plz help to me solve this error
May 15 '07 #1
3 1502
prn
254 Expert 100+
Hi archulu,

Your problem is that $frag is declared within a block delimited by the {} following the "if". Therefore that declaration "my $frag" is local to that block, so when you reference $frag later outside its scope, the Perl interpreter (correctly) complains. You need to declare $frag globally if you need to use it globally. (And don't redeclare it within the block or you will be assigning to a different variable with the same name and local scope!):
Expand|Select|Wrap|Line Numbers
  1. my $frag;
  2. if ($a < 15)
  3. {
  4.   $frag = substr $string, 0, 4;
  5. }
  6. ...
  7. my $sub=$frag.$frag1.$frag2;  
  8. #There's no need to concatenate null strings here.
HTH,
Paul
May 15 '07 #2
KevinADC
4,059 Expert 2GB
Paul has explained the error message. But there is also no need for your if/else block at all. Use the ternary operator to define the value of $a (changed to $length below). You really should not use $a or $b except with the sort() function.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $string = 'N/W_Administrator/Trainee';
  5.  
  6. my $str = 'Level1';
  7. my $len1 = 1;
  8.  
  9. my $length = length($string) < 15 ? 4 : 7; 
  10. my $frag = substr $string, 0, $length;
  11. print "fragment: $frag\n";
  12.  
  13.  
  14. my $frag1 = substr $str, 0, $len1;
  15. my $frag2 = substr $str, 5;
  16. my $sub   = "$frag$frag1$frag2";
  17.  
  18. print "frag1: $frag1\n";
  19. print "frag2: $frag2\n";
  20. print "values are: $sub\n"; 
May 15 '07 #3
archulu
34
thank u so much sir it very help to me
May 16 '07 #4

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

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
8
by: mrstephengross | last post by:
I'm using gcc 3.3.1 to compile the following code (below). I've written a macro to simplify writing operators. The macro uses the '##' operator to paste together 'operator' and the name of the...
9
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
5
by: Edwin Smith | last post by:
Hello: I have a problem building a file command line argument from 2 cells in a table with the following code. string filePath =...
3
gregerly
by: gregerly | last post by:
Hello, I think this should be a simple answer, but I haven't found any good resource yet. I have an element that onclick I run a function, passing a reference to the object clicked. I then grab...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
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...

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.