Unix Perl split special character $

All I'm trying to split a string at the $ into arrays

@data:=<dataFile>
a $3.33
b $4.44
dfg $0.56

The split command I have been playing with is:
split(/\$/, @data)

which results with
a .33 b .44 dfg .56

any help with this is appreciated

/r
Rick

$
$ cat data1.txt
a $3.33
b $4.44
dfg $0.56
$
$ perl -ne 'chomp; split /\$/; push @a1,$_[0]; push @a2,$_[1]; END{foreach $i(@a1){print $i,"\n"} foreach $i(@a2){print $i,"\n"}}' data1.txt
a
b
dfg
3.33
4.44
0.56
$

tyler_durden

You can't use split() on an array:

split(/\$/, @data);

Well, you can but it won't return what you expect.

So I assume you didn't actually do that, post the actual code you tried because if you split those lines using '$' as the argument you will get what you expect.

while(<DATA>){
	my @tmp=split('\$',$_);
	print join ", ", @tmp;
}
__DATA__
a $3.33
b $4.44
dfg $0.56

-----Post Update-----

while(<DATA>){
	my @tmp=split('\$',$_);
	print join ", ", @tmp;
}
__DATA__
a $3.33
b $4.44
dfg $0.56

The actual code I used was a simple test string...

$test="This is a test $6.56 $5.7";
print split(/\$/, $test);

which resulted the

This is a test .56 .7

However with your example the test works fine... I hope you could shed some light on this..

while<FILE>{
chomp;
my ($stampItem $stampPrice)=split(/\$/);
push @stampItem, $stampItem;
push @stampPrice, $stampPrice;
}

/r
Rick

Your test string is flawed:

$test="This is a test $6.56 $5.7";
print $test;

See the problem with your test?

I never seem to give enough information when I post.. sorry about that.

The test string, if I ever got it to seperat correctly, was going to seperat the dollar amount and assigne it to an array and then have the money value added for a total.

$test="This is a test $6.56 $5.7";
print split(/\$/, $test);

which resulted the

This is a test .56 .7

This was a simple design to see why my split did not work correctly.

With your snipit all came together:

open(FILE, "<", "stamp_list");
my @stampItem;
my @stampPrice;
while(<FILE>) {
chomp;
my ($stampItem, $stampPrice)=split(/\$/);
push @stampItem, $stampItem;
push @stampPrice, $stampPrice;
}

print("\n\n\t\tCurrent Stamp Values\n\n");

$arrayLength=@stampItem;

for($i=0; $i<$arrayLength; $i++) {
print("$stampItem[$i] \$$stampPrice[$i]\n");
}

for($i=0; $i<$arrayLength; $i++) {
$totalValue += $stampPrice[$i];
}

print("\n\nTotal value of stamps is: \$$totalValue\n\n");

stamp_list
stamp name 1 $3.50
stamp name 2 $5.25
...

again I don't understand why my simple test failed and only showed the change and not the full dollar amount.

/r
Rick

here's another way.. you don't split on $ but split normally on spaces. then go through each element and check for dollar sign. add them to get total


$test="This is a test \$6.56 \$5.7";
@a=split /\s+/,$test;
$total=0;
foreach my $item (@a){
 if ( $item =~ /\$/){
  $item=~s/\$//;
  $total=$total+$item;
 }
}
print $total;

I guess you failed to understand the problem and didn't bother to even run my sample code to show you the problem. So now I will spell it out for you.

When perl sees '$' in a double-quoted string it is going to interpolate it and expand it into its value. Example:

$foo = 'beginner';
$string= "You are a $foo";
print $string;

If you notice when it prints there is no $ in the output but the word 'beginner' where the scalar $foo was.

Well in your string:

$test="This is a test $6.56 $5.7";

perl think $6 and $5 are scalars and expands them, but they have no values, so this what the string interpolates to:

This is a test .56 .7

If you split that on the '$' character, which is not in the string, you will get one string back, the original string.

When you open a file and parse the lines of a file perl treats that the same as a single-quoted string:

$test= 'This is a test $6.56 $5.7';

No variable interpolation/expansion so now the literal string is:

This is a test $6.56 $5.7

Which when split on the '$' character returns the values you expect.

Moral of the story.... learn the fundamentals of perl, which is the scalar and the string.

Thank you for taking the time explain what now seems such a simple question. These basic exercises do prove challenging for new students to the perl language. But with the help of the great world of the internet even us newbies don't have to feel like idiots when learning the fundamentals. Can�t wait to take the class on Advanced Unix and Shell scripting.

BTW.. I did run your script and it worked as advertised... just did not understand why my script didn't.. Thanks for your help.