Unix Perl split special character $


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Perl split special character $
# 8  
Old 06-09-2009
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
Code:
$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;

# 9  
Old 06-09-2009
Quote:
Originally Posted by schultz2146

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


Rick
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:

Code:
$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:

Code:
$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:

Code:
$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.
# 10  
Old 06-09-2009
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.
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