Unix Perl split special character $


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Perl split special character $
# 1  
Old 06-07-2009
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
# 2  
Old 06-07-2009
Code:
$
$ 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
# 3  
Old 06-07-2009
Quote:
Originally Posted by schultz2146
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
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.
# 4  
Old 06-08-2009
Code:
while(<DATA>){
	my @tmp=split('\$',$_);
	print join ", ", @tmp;
}
__DATA__
a $3.33
b $4.44
dfg $0.56

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

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

# 5  
Old 06-08-2009
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

Last edited by schultz2146; 06-08-2009 at 04:38 PM..
# 6  
Old 06-08-2009
Your test string is flawed:

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

See the problem with your test?
# 7  
Old 06-09-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

2. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Shell Programming and Scripting

Character Find and replace in Unix or Perl

Hi, I am stuck with an problem and want some help, what i want to do is There is a directory name temp which include file named t1.txt, t2,txt, t3.txt and so on. These files contains data, but there are some bad character also that is % present in the files , I want to write the script... (13 Replies)
Discussion started by: parthmittal2007
13 Replies

4. Shell Programming and Scripting

Split a special char

Hello, I have some data in output file.In that i need to split the special char "(" and ")" and store it. This is example of o/p file. (OHC12345) (OHC12415) (OHC12765) (OHC12545) I need like OHC12345 OHC12415 OHC12765 OHC12545 --Thanks (5 Replies)
Discussion started by: rasingraj
5 Replies

5. Shell Programming and Scripting

Deleteing one character after an special character

I have below line in a unix file, I want to delete one character after "Â". 20091020.Non-Agency CMO Daily Trade Recap Â~V Hybrids The result should be : 20091020.Non-Agency CMO Daily Trade Recap  Hybrids i dont want to use "~V" anywhere in the sed command or any other command, just remove... (1 Reply)
Discussion started by: mohsin.quazi
1 Replies

6. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

7. Shell Programming and Scripting

Perl use split and remove specific character

i want to split the input by a space and remove specific characters like full stop, comma...... etc. and then save each word in an array. i got something below, but it didn't work. can anyone please help me? Thank you #!/usr/bin/perl -w while (<>) { $line = <>; @word = split(' ',... (6 Replies)
Discussion started by: mingming88
6 Replies

8. UNIX for Advanced & Expert Users

Escaping special character stored in variables : perl

Hi just for regular use i m working on small module written in perl for getting date in specified format like i have to specify date format and then seperator to seperate date i am 95% done. now i m sure explanation i gave is not good enough so i am putting output here : C:\Documents and... (2 Replies)
Discussion started by: zedex
2 Replies

9. Shell Programming and Scripting

Perl Script Syntax to Extract Everything After Special Character

Hi, I am writing a Perl script that reads in many lines, if a line meets the criteria I want to edit, it. For example, the script will return the following example line... test=abc123 All I want to do is strip off the "test=" and just be left with the abc123. In my script I can easily... (3 Replies)
Discussion started by: edrichard
3 Replies

10. UNIX for Dummies Questions & Answers

perl split funciton - special character "/"

HI, I have a directory structure. /abc/def/ghi/ I want to store it into array. So that if I do a pop function on that array I can easily go to previous directory. But how can i split and store it. @Directory = split(/\//,$DirectoryVarialbe) That doest works. Any other escape sequence... (5 Replies)
Discussion started by: deepakwins
5 Replies
Login or Register to Ask a Question