Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Split using special charater data type scalar

by kapila (Acolyte)
on Mar 25, 2013 at 12:55 UTC ( #1025288=perlquestion: print w/ replies, xml ) Need Help??
kapila has asked for the wisdom of the Perl Monks concerning the following question:

I actually want to separate & get values in array for below example.1.74,1.75,test/document.txt

But its working fine when i run standalone but in my app some issue which i am not able to figure out. When i hard code $aa='1.74 --> 1.75'; then i can split. But under this code its not splitting, even backslash with (--\>) also tried but no help. I have used ref(\$aa) also its showing SCALAR. Not sure why i am not able to split $aa. can experts help me here.

$text='--------------- 1.74 --> 1.75 :test/document.txt; sub func { my ($text) = (@_); $text =~ s/[\s|\n]*//g; $text =~ s/^[---]+//; my ($aa,$cd)=split(/:/,$text); my ($values,$cf)= split(/-->/,$aa); print "$values,$cf\n";

Comment on Split using special charater data type scalar
Download Code
Re: Split using special charater data type scalar
by hdb (Prior) on Mar 25, 2013 at 13:04 UTC
    $text='--------------- 1.74 --> 1.75 :test/document.txt'; sub func { my ($text) = (@_); $text =~ s/[\s|\n]*//g; $text =~ s/^[---]+//; my ($aa,$cd)=split(/:/,$text); my ($values,$cf)= split(/-->/,$aa); print "$values,$cf\n"; } func($text);

    prints "1.74,1.75". Which it should????

Re: Split using special charater data type scalar
by aitap (Deacon) on Mar 25, 2013 at 13:18 UTC

    Please provide a minimal code example which actually can be complied, run and shows your problem. When I fixed syntax errors in your code, it actually ran and splitted your string into pieces:

    use warnings; use strict; my $text='--------------- 1.74 --> 1.75 :test/document.txt'; sub func { my ($text) = (@_); $text =~ s/[\s|\n]*//g; $text =~ s/^[---]+//; my ( $aa, $cd ) = split( /:/, $text ); my ( $values, $cf ) = split( /-->/, $aa ); print "$values,$cf\n"; } func($text); __END__ 1.74,1.75
    It looks like what you wanted, isn't it?

    By the way, if you want just to extract these two values from this string, it may be better to match them, not to split:

    my $str = '--------------- 1.74 --> 1.75 :test/document.txt'; my ($values,$cf) = $str =~ /([\d.]+)\s*-->\s*([\d.]+)/; print "$values,$cf\n"; __END__ 1.74,1.75
    What I used is m/PATTERN/ operator described in perldoc perlop -> Regexp Quote-Like Operators in array context (with Capture groups).

    Sorry if my advice was wrong.
Re: Split using special charater data type scalar
by jwkrahn (Monsignor) on Mar 25, 2013 at 18:42 UTC
    $text =~ s/[\s|\n]*//g;

    If we expand the \s (whitespace) character class we get:

    $text =~ s/[ \t\n\r\f|\n]*//g;

    So why are you including the \n (newline) character twice?    And why are you including the | (vertical bar) character, which has nothing to do with whitespace?    And matching zero characters is inefficient.    What you probably want is:

    $text =~ s/\s+//g;


    $text =~ s/^[---]+//;

    A character class ignores multiple identical characters so what you effectively have is:

    $text =~ s/^[-]+//;

    Which could be written more simply without the character class:

    $text =~ s/^-+//;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025288]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-10-25 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls