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

unpack format string for ignoring chars

by LanceDeeply (Chaplain)
on Feb 13, 2004 at 21:20 UTC ( #328882=perlquestion: print w/replies, xml ) Need Help??

LanceDeeply has asked for the wisdom of the Perl Monks concerning the following question:

guys-

is there a way to ignore characters you are unpacking?

unpackin(); sub unpackin { my $text = 'girl,you,really,got,me,now,you,got,me,so,i,dont,know,w +hat,im,doin,666,777,99.99'; my @ar = split /,/, $text ; my @lens = map { length } @ar ; my $index = 0; my $format = ''; foreach my $len (@lens) { $format .= "A$len" . 'A'; } my $odd= 1; my @words = grep { ($odd) ? ($odd=0)+1 : ($odd=1)-1 ;} unpack($for +mat, $text ); print "( @ar )\n"; print "( @lens )\n"; print "format = $format\n"; print join "|", @words; print "\n"; }

produces

( girl you really got me now you got me so i dont know what im doin 66 +6 777 99.99 ) ( 4 3 6 3 2 3 3 3 2 2 1 4 4 4 2 4 3 3 5 ) format = A4AA3AA6AA3AA2AA3AA3AA3AA2AA2AA1AA4AA4AA4AA2AA4AA3AA3AA5A girl|you|really|got|me|now|you|got|me|so|i|dont|know|what|im|doin|666| +777|99.99
i'd love to be able to do this without having to skip every other element in the result aray...

thanks

Replies are listed 'Best First'.
Re: unpack format string for ignoring chars
by BrowserUk (Pope) on Feb 13, 2004 at 21:32 UTC

    Like this?

    print join '|', unpack '( a4 x10 )2 a4', 'this not that this not that +this'; this|this|this

    Or was it like that:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!
      thanks!

      i just didnt know how to use x properly, got a little confused reading the doc.
      i added x1 to the format string wherever i wanted to ignore a character
      my $count = 0; foreach my $len (@lens) { if ( $count ++ ) { $format .= "x1"; } $format .= "A$len"; }
      does the deed nicely

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2021-05-13 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (134 votes). Check out past polls.

    Notices?