Re: Etxraction of numbers in an array
by 1nickt (Canon) on Nov 24, 2016 at 11:52 UTC
|
There are at least 42 ways.
Please show what you've tried, and how it didn't work. You might be close!
Edit: OK, thanks for your code.
If you just want to extract numbers, you could use a simple regular expression instead of split() (which you would have to call more than once to get your desired results from your string):
my @x = ( $string =~ /\d+/ga );
Note the use of g to match all occurrences, and a to force the regexp to match only ASCII numbers with the /d character class.
You should familiarize yourself with the basics of regular expressions: perlrequick is a good place to start.
Finally, may I ask why your data is in this format? It smells like an XY problem and there are probably better ways to create your input data. What is the task you are really trying to accomplish?
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
|
| [reply] |
Re: Etxraction of numbers in an array
by BrowserUk (Patriarch) on Nov 24, 2016 at 12:44 UTC
|
t-rex, you will have to try a bit harder with your posts. With 47 posts, you've been around long enough that by now you should have at least mastered some of the syntax and terminology.
I have the following string: A1=<m,n>:<p,q>:<s,t>...go on
- Is all of that stuff in code brackets the "string"?
- Or, is the A1= part meant to indicate that the rest is assigned to a variable called "A1"?
If so, where are the quotes ('' or "") around the "string"?
Where is the sigil($) on the variable name?
- Is the "..,go on" also part of the string?
Or is that meant to indicate that the :<n,m> bit can be repeated more times?
If so, don't you mean "so on"?
And wouldn't it be clearer if you put that bit outside of the code brackets!?
I have to extract values m,n,p,q,s,t from the above array.
- Why has the "string" suddenly become an "array"?
- Are m,n, etc. just single alphabetical characters? Or perhaps single digit numbers? Or multi-digit numbers?
How do i do it
After 6 months here you haven't achieved any enlightenment as to where to start?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
I understood your point, I code mostly in assembly language and have to use perl intermittently with harsh deadlines, so I accept i posted this hastily, now I have corrected my post, please have a look, sorry for inconveneince
| [reply] |
|
#! perl -sw
use strict;
my $string = "<1,2>:<5,7>:<3,0>";
my @x = $string =~ m[<(\d+),(\d+)>]g;
print "Extraction = @x\n";
print "1st element = $x[0]";
__END__
c:\test>junk
Extraction = 1 2 5 7 3 0
1st element = 1
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re: Extraction of numbers in an string
by johngg (Canon) on Nov 24, 2016 at 14:14 UTC
|
You could split into pairs on a '<' or a '>' optionally followed by ':<' then split the pairs on a comma.
johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E '
my $str = q{<1,2>:<5,7>:<3,0>};
my @nos = map { split m{,} } split m{(?x) < | > (?: :< )?}, $str;
say for @nos;'
1
2
5
7
3
0
I hope this is helpful.
Update: Changed quantifier from * to ?.
| [reply] [d/l] [select] |
Re: Etxraction of numbers in an array
by Erez (Priest) on Nov 24, 2016 at 12:47 UTC
|
I have to extract values m,n,p,q,s,t from the above array
First thing, that is not an array, at least not in Perl. I'm not familiar with any language that has that array syntax. So it's hard to even hazard a guess. From just looking at the thing, it might involve split by the : character, but again, I'm really drawing a blank here.
Any further context you can give regarding this will probably benefit you with more concise answers.
Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert
| [reply] |
|
| [reply] |
Re: Extraction of letters from a string
by hippo (Archbishop) on Nov 24, 2016 at 13:17 UTC
|
#!/usr/bin/env perl
use strictures;
my @v = 'A1=<m,n>:<p,q>:<s,t>...go on' =~ /[mnpqst]/g;
| [reply] [d/l] [select] |
|
here the m,n,p,q... are numbers which can change based on the user, so writing a regex just to hardcode with numbers won't be a good idea i guess
| [reply] |
|
How was m,n,p etc. supposed to represent digits? Anyway, if you want to match digits, match digits:
#!/usr/bin/env perl
use strictures;
my @v = 'A1=<9,8>:<7,6>:<5,4>...go on' =~ /\d/ag;
| [reply] [d/l] |
|
|
|
Re: Extraction of numbers in an string
by karlgoethebier (Abbot) on Nov 25, 2016 at 11:23 UTC
|
karls-mac-mini:Desktop karl$ perl -E "say for grep {/\d/} split '', q(
+<1,2>:<5,7>:<3,0>);"
1
2
5
7
3
0
It seems like this works. Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] |
|
c:\@Work\Perl\monks>perl -wMstrict -le
"printf qq{$_ } for grep {/\d/} split '', q(<1,22>:<5,7>:<333,0>);
"
1 2 2 5 7 3 3 3 0
Update: Actually, if one were married to split, it could be made to work with a rather more complex regex:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le
"my @d = grep length, split qr{ >:< | [<,>] }xms, q(<1,22>:<5,7>:<333,
+0>);
dd @d;
"
(1, 22, 5, 7, 333, 0)
But I wouldn't recommend it.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
#!/usr/bin/perl -l
# http://perlmonks.org/?node_id=1176478
use strict;
use warnings;
my $string = '<1,22>:<5,7>:<333,0>';
my @x = grep /\d/, split /\D+/, $string;
print for @x;
| [reply] [d/l] |
|
|
|
|
|
| [reply] |