Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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";

Replies are listed 'Best First'.
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/^-+//;
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????

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?
Lady_Aleena clean up after dinner finally.
[Discipulus]: uch! so we are twice lucky to have MCE..;=)
[marioroy]: Working a midnight graveyard shift, on the way home ran out of gas. just 2 minutes later somebody stops with a small tank of gas. It's wierd. There's always someone nearby for some reason.
[Lady_Aleena]: I was the one who stop[ed in the middle of a cold night to pick up a small family walking to a gas station. I got a few gallons of free gas for the round trip, and the grandmother said she'd pray for me.
[marioroy]: Folks do not know it, but MCE was ridiculously difficult. At Niagra Falls, heard a voice on enabling Grace while in the mist boat. That 10x performance is possible.
[marioroy]: Lady_Aleena Angels on earth are real people. That's awesome.
[Lady_Aleena]: marioroy, what does MCE stand for in this instance? There are 3 pages of abbreviations to go through.
[Lady_Aleena]: ^ or Discipulus...
[marioroy]: Many-core Engine
[choroba]: MCE

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2017-05-29 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?