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!

String Manipulation in Array 2

Status
Not open for further replies.

hello99hello

Programmer
Jul 29, 2004
50
0
0
CA
Hello All

I have the string below:

Red Cross, CPR – life support, completed

I have managed to put the string in an array separated by comma.

My goal is to do three things:

1)use the default value in third element of the array, else use “completed”
if ($array[0] eq ?)
else $array[0] = “completed”;
2)merge second and third elements into first element so that it reads:Red Cross – CPR Support – completed
$array[0] = $array[0] . “-“ $array[1]. “-“ $array[2]
3)Remove the last two elements after merging:
chop(@array);
chop (@array);

However, this is not working, any input would be greatly appreciated.
 
Can we see the actual code that's "not working," instead of little bits of pseudocode?
 
Like mikevh, I am not quite sure what you are trying to accomplish, but here's a snippet that may help you. (I'm assuming you want the third field to default to "completed" only if it is left blank...)
Code:
$input="Red Cross, CPR - life support, completed";
@array=split(",",$input);
$array[0]="completed" if (! defined $array[0]);
@array= ("$array[0] - $array[1] - $array[2]");
print "$array[0]\n";
print "@array\n";  #same thing...
If you run this code, you can see that both lines that are printed are the same becuase the there is only one element in the array. If you still want to go about it your way, you could do this instead:
Code:
$input="Red Cross, CPR - life support, completed"; 
@array=split(",",$input);
$array[0]="completed" if (! defined $array[0]);
$array[0] = ("$array[0] - $array[1] - $array[2]");
print "$array[0]\n";
pop(@array);
pop(@array);
print "@array\n";
Both lines output from this are identical as well. Note that you can use the pop function instead of chop to remove the last element. (chop would get rid of the last character, not the last element). You could also use: undef($array[$#array]) to get rid of the last element.
But, if I were you, I would do it this way:
Code:
$input="Red Cross, CPR - life support";
$input=~s/^(.*,.*[^,]),?$/$1,completed/;
$input=~s/,/ - /g;
print "$input\n";
The second line checks to see if the 3rd field is missing - if it is, then it will add "completed". It can handle this string:
$input="Red Cross, CPR - life support"; (no comma at the end)
or this string:
$input="Red Cross, CPR - life support,"; (comma at the end).
or obviously this string:
$input="Red Cross, CPR - life support, completed";

The third line then changes all commas to dashes.
This way you don't have to fill an array, manipulate the string in array, and convert back to a string.

Hope this helps!

Brian
 
One quick fix to the last code segment I posted above: Replace the dot (matches any character) with [^,] (matches any character except a comma). The way I posted above is a greedy regular expression, and some cases may fool it. This code snippet is not greedy and should handle any cases.

Code:
perl -e '$input="Red Cross, CPR - life support,completed";
$input=~s/^([^,]+,[^,]+),?$/$1,completed/;
$input=~s/,/ - /g;
print "$input\n";'

That works better...
I am matching this:
any amount of characters followed by a comma, followed by any amount of characters, followed by either zero or one commas.

Thanks,

Brian
 
Thank you all for your input.

I have solved all the problems with your advises

Merci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top