Solved

map

Posted on 2006-06-21
12
204 Views
Last Modified: 2010-03-05
I have
$output = "X123456 X234567 X234568
X123452 X234562 X234561
X123422 X234533 X234522";

how can i grep only the 5 digits and save it in array @codeword

can i do using map?
0
Comment
Question by:perlperl
12 Comments
 

Author Comment

by:perlperl
ID: 16954737
i tired

@output = split (/ /, $output);
my @codeword = map { /(X\d{5})/ } @output;

teh output i get, is
234567 234568 234562 234561 234533 234522


0
 

Author Comment

by:perlperl
ID: 16954742
sorry the output was
123456 234567 234568 234562 234561 234533 234522


it skips the first element forn second row onwards
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 500 total points
ID: 16954849
Hi,

try

@output = split (/\s/, $output);


---
Harish
0
 

Author Comment

by:perlperl
ID: 16954893
if i try  \s

teh output i get is
234567 234568  123452 234562 234561 1231422 234533 234522

i get everything except the first elemnt 123456


just clarification, i m using
my @codeword = map { /(X\d{6})/ } @output;


there was typo above where i specified 5
0
 

Author Comment

by:perlperl
ID: 16954900
this worked fine but i don't like this format

@output = split(/\n/,$output);
for($i = 0; $i < @output; $i++) {
 @code = split(/ /, $output[$i]);
 @c = map { /(X\d{6})/ } @code;
@subset_ucode = ( @subset_ucode ,@c);
}
print "@subset_ucode \n";
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16954918
I got this:
X12345 X23456 X23456 X12345 X23456 X23456 X12342 X23453 X23452


the code was:

$output=  "X123456 X234567 X234568
X123452 X234562 X234561
X123422 X234533 X234522";

@output = split (/\s/, $output);
@codeword = map { /(X\d{5})/ } @output;

print "@codeword";

If you want the digits, then change

@codeword = map { /(X\d{5})/ } @output;

to

@codeword = map { /X(\d{5})/ } @output;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:perlperl
ID: 16954938
oops sorry my bad...

that works
0
 

Author Comment

by:perlperl
ID: 16954945
why \s worked.
can u pls explain
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16954949
\s means whitespace. It includes space, tab and newline character
0
 

Author Comment

by:perlperl
ID: 16954958
Got it!!!

Thanks a lot
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16955028
$output = "X123456 X234567 X234568
X123452 X234562 X234561
X123422 X234533 X234522";

@codeword = $output =~ /(\d+)/g;

print "@codeword";

0
 
LVL 84

Expert Comment

by:ozo
ID: 16965246
what exactly do you want in @codeword?  you say you want only the 5 digits, but everything in $output  looks like it has 6 digits
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to send output of ping to syslog (with timestamp and optional comments) 3 186
Perl output collect 7 65
Using Perl to parse rows 7 93
Move Function in Perl Script 2 65
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now