Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split array by special character and getting values

Status
Not open for further replies.

dmazzini

Programmer
Jan 20, 2004
480
0
0
US
Code:
@values= (value1|value2|value3|value4|value5|value6|value7);

Elements of the array @values can have spaces in the middle, e.g:

value6 = "C:\Documents and Settings\dmazzini\Desktop\my dir\my first.xls";

How I can split up array @values by "|" and assign  values to variables, like

$var1=value1;
$var2=value2;
.
.
.
.
$var6=value6;   (Of course exactly as it is, with spaces in the middle, etc)
$var7- value7


dmazzini
GSM/UMTS System and Telecomm Consultant

 
@splitvalues = split(/|/,@value);

You can get the individual values by:

$splitvalues[0]

The number in the bracket corresponds to the number in the array starting with 0.

[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
Is it an array or a string? If it really is an array then you don't need to split anything. Just access whatever item in the array that you want.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Here is code that would actually run assuming you have a string to start with.

use strict;
use warnings;


my $values= "value1|value2|value3|value4|value5|value6|value7";
my @splitvalues = split(/\|/,$values);
print $splitvalues[1];

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi guys

I had tried it and it did not work.

I have attached a piece of code.

Code:
#!/usr/bin/perl
# Receiving arguments from PowerBuilder GUI
# March 31, 2007

use strict;
use Tk;
use Win32;
use Win32::API;

my ($flag_dcn,$flag_coco,$action,$option,$trs_xlsfile,$dropdown_value,$dropdown_country);
my (@collapsed,@arguments,$collapsed, $i,@arguments);


@arguments = @ARGV;

Win32::MsgBox("@arguments",0,'test'); # at this point I have in @arguments the whole array separated by |.


# Proble start here
@arguments = split(/|/,@arguments);

$flag_dcn= $arguments[0];
$flag_coco= $arguments[1];
$action= $arguments[2];
$option= $arguments[3];
$dropdown_value= $arguments[4];
$dropdown_country = $arguments[5];
$trs_xlsfile= $arguments[6];

Win32::MsgBox("DCN:$flag_dcn,COCO:$flag_coco,ACTION:$action,OPTION:$option,Excel:$trs_xlsfile,SiteManagerVersion:$dropdown_value,$dropdown_country",0,'test'); 


# ##################################################################
# Routine Trim. Remove Blank Spaces Left-Right
# ##################################################################

sub trim {
    my @out = @_;
    for (@out) {
        s/^\s+//;          # trim left
        s/\s+$//;          # trim right
    }
    return @out == 1
              ? $out[0]   # only one to return
              : @out;     # or many
}

dmazzini
GSM/UMTS System and Telecomm Consultant

 
try this:

#!/usr/bin/perl
# Receiving arguments from PowerBuilder GUI
# March 31, 2007

use strict;
use Tk;
use Win32;
use Win32::API;

my ($flag_dcn,$flag_coco,$action,$option,$trs_xlsfile,$dropdown_value,$dropdown_country);
my (@collapsed,@arguments,$collapsed, $i,@arguments, $args);


$args = @ARGV;

Win32::MsgBox("$args",0,'test'); #

# Proble start here
@arguments = split(/|/,$args);

$flag_dcn= $arguments[0];
$flag_coco= $arguments[1];
$action= $arguments[2];
$option= $arguments[3];
$dropdown_value= $arguments[4];
$dropdown_country = $arguments[5];
$trs_xlsfile= $arguments[6];

Win32::MsgBox("DCN:$flag_dcn,COCO:$flag_coco,ACTION:$action,OPTION:$option,Excel:$trs_xlsfile,SiteManagerVersion:$dropdown_value,$dropdown_country",0,'test');


# ##################################################################
# Routine Trim. Remove Blank Spaces Left-Right
# ##################################################################

sub trim {
my @out = @_;
for (@out) {
s/^\s+//; # trim left
s/\s+$//; # trim right
}
return @out == 1
? $out[0] # only one to return
: @out; # or many
}


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi

Expression

Code:
$args = @ARGV;

It does not work. not possible to convert array to string directly.

Basically to split an string is quite easy. But, I hav eto split arguments received (@ARG) by | operator.

e.g.

Code:
@ARGV= qq(one|two|three with spaces|four|five);

If I split @ARGV, values with spaces will be considered as more than one element.









dmazzini
GSM/UMTS System and Telecomm Consultant

 
Ya my bad on the $args = @ARGV. try $args = $ARGV[0].

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
$args = $ARGV[0].

Take just first element of the array, problem is for element with spaces.

dmazzini
GSM/UMTS System and Telecomm Consultant

 
Your example doesn't have any spaces in it.
It is pipe seperated.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
travs, elements in @ARGV are separated by "|" operator. But elements mya have space

e.g.

@ARGV = (red| yellow an blue| pie| etc)

However I just found the solution:

Code:
my $arguments = qq(@ARGV); # It did the trick

($flag_dcn,$flag_coco,$action,$option,$dropdown_value,$dropdown_country,$trs_xlsfile) = split(/\|/,$arguments);

Thanks everyone

dmazzini
GSM/UMTS System and Telecomm Consultant

 
I think that
Code:
my $arguments = join('', @ARGV);
would work too, and it makes it clear to the reader what you are doing.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Using join, we can convert @ARG to a variable. But I have tested it and it took out spaces.

e.g

if one of the elements have a value like |XXX YYY ZZZ|. at the end it returns XXXYYYZZZ.



dmazzini
GSM/UMTS System and Telecomm Consultant

 
Make sure you don't have a space between the tick's after join
join('', @ARGV) is correct
join(' ', @ARGV) is not

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I have a question - have you printed @ARGV near the top of your script and had a look at what you're working with? I suspect you're piping the data into this script and the OS is splitting the arguments on spaces for you.

Something like this might work for you as well:
Code:
($var1,$var2...$varN) = split /\s*\|\s*/, join(' ', @ARGV);
 
this is several arguments:

perl filename var|var var|var|var

maybe you should do this instead:

perl filename "var|var var|var|var"

now you send one argument to your script which will be in ARGV[0] and you can use split() like you want,



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin

Actually it is a good idea :-|
I should think about it from the very beginning.. :-|


Anyway, it 's working now. But your suggestions were good

The main issued was that I am calling this perl script from PowerBuilder, which I don't know too much and it took me some time to learn how to collect and pass this variables to perl.

I still love perl. jejeje!

thanks




dmazzini
GSM/UMTS System and Telecomm Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top