Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Parsing a long string

by BrowserUk (Patriarch)
on Aug 16, 2013 at 19:08 UTC ( [id://1049771]=note: print w/replies, xml ) Need Help??


in reply to Parsing a long string

$s = '1START ACCOUNTA XXXXXMA 12345 XYZ111 1ST +ART ...';; print $1 while $s =~ m[((?:\S+\s+){5})]g;; 1START ACCOUNTA XXXXXMA 12345 XYZ111 1START ACCOUNTB XXXXXBR 12345 XYZ191 1START ACCOUNTA XXXXXNY 54321 XYZ131

Or as the records appear to be fixed length:

[0] Perl> print $1 while $s =~ m[(.{61})]g;; 1START ACCOUNTA XXXXXMA 12345 XYZ111 1START ACCOUNTB XXXXXBR 12345 XYZ191 1START ACCOUNTA XXXXXNY 54321 XYZ131

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.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Parsing a long string
by Anonymous Monk on Aug 16, 2013 at 19:16 UTC
    They are one long line of characters. I cannot use fixed length, I have to use "substr" to find the values I need and to the logic then.
      I cannot use fixed length, I have to use "substr" to find the values I need ...

      I don't understand. All the examples of substr that appear in the OPed code use fixed offsets and lengths (at least, I assume you're the AnonyMonk who posted the OP). Sticking with fixed-width fields, here's another approach that might serve your needs — although I'm not really sure what those needs are! See pack for info on template specifiers; the  '@' specifier in an unpack template moves to an absolute position. Note that the  'x' specifier makes relative forward moves if you can figure out the relative displacements needed; this will save some absolute back-and-forthing. Also see perlpacktut.

      >perl -wMstrict -lE "my $data = 'ACCTAxACCTBxACCTCxxxFOOxxBARxxBAZxxBOFFxxxxx'; say qq{'$data'}; ;; my ($fr1, $fr2, $fr3, $at20, $at25, $at30, $at35) = unpack '@0 a5 @6 a5 @12 a5 @20 a3 @25 a3 @30 a3 @35 a4', $dat +a; ;; say qq{'$fr1' '$fr2' '$fr3'}; ;; my $account = $fr3; ;; my $value = ($account eq 'ACCTC') ? $at20 : 'unknown'; my $end_value = ($value eq 'FOO') ? $at25 : $at30; ;; say qq{account '$account' value '$value' end value '$end_value'}; " 'ACCTAxACCTBxACCTCxxxFOOxxBARxxBAZxxBOFFxxxxx' 'ACCTA' 'ACCTB' 'ACCTC' account 'ACCTC' value 'FOO' end value 'BAR'

      Update: Changed example code to simplify logic.

      I have to use "substr" to find the values

      No. You don't. As I demonstrated. But s'your choice ...


      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.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1049771]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found