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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using map to create an array 1

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
0
0
IL
HI,

I need to split a line of data into an array (data sits in $_):
"08/26/2008","0.000000","4.000000","332.000000"

tried:
@nameX = map { s/.*\((.*?)\)/$1/ } split /,/,$_ ;

My array needs to be :
08/26/2008 0.000000 4.000000 332.000000
I get an empty array...

Will appreciate ideas.
thanks

Long live king Moshiach !
 
there are two ways of doing this.. no map needed

1.
Code:
use strict;
use Text::CSV;
my $csv = Text::CSV->new ();
my $string = qq["08/26/2008","0.000000","4.000000","332.000000"];
my $status = $csv->parse($string);
my @columns = $csv->fields();
foreach ( @columns ) {
  print "$_\n";
}


the result is the following

08/26/2008
0.000000
4.000000
332.000000

2.
Code:
use strict;
my $string = qq["08/26/2008","0.000000","4.000000","332.000000"];
$string =~ s/^"//;
$string =~ s/"$//;
my @columns = split '","', $string;
foreach ( @columns ) {
  print "$_\n";
}


the result is the following

08/26/2008
0.000000
4.000000
332.000000



``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
It certainly can be done in a one-liner with 'map'. Using Text::CSV is far more robust, however. For instance, what if there's a comma in one of your fields (i.e. between the quotes)? Using 'split' and 'map' would fail in that circumstance.
 
The fastest should be
[tt]@nameX=map{substr($_,1,-1)}split/,/;[/tt]
otherwise
[tt]@nameX=map{s/"//g;$_)}split/,/;[/tt]
or even (very slow)
[tt]@nameX=map{eval}split/,/;[/tt]
However I agree that you should be certain of what is in your data, before using the above.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
sorry mate...I didn't know you wanted it in one line..
there you go
Code:
$string = qq["08/26/2008","0.000000","4.000000","332.000000"];
@columns = map {  s/^"//; $_ }  map {  s/"$//; $_ } split  ',', $string;


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Thanks to all,

and if I wanted to fill the array with text between the ( ) barckets (processname) :

"(PDH-CSV 4.0)","\\pps01\Process(Idle)\ID Process","\\pps01\Process(System)\ID Process"

how would the one liner look ?
thanks



Long live king Moshiach !
 
I'm not sure if I follow...do you want the result to be
@columns = ('PDH-CSV 4.0','Idle','System')

if yes then the following will do the job...

Code:
$string = qq["(PDH-CSV 4.0)","\\pps01\Process(Idle)\ID Process","\\pps01\Process(System)\ID Process"];
@columns = map {  /\((.*?)\)/; $1 } split  ',', $string;

if you mean something else..then please be more specific with samples and examples


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top