Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

delirium's scratchpad

by delirium (Chaplain)
on Jun 01, 2004 at 23:28 UTC ( [id://358804]=scratchpad: print w/replies, xml ) Need Help??

Home-grown CSV file splitter

#!/usr/bin/perl use strict; sub csv_split { local $_ = shift || return undef; my @array = (); my $count = my $quoted = 0; while ( s/(.)// ) { my $char = $1; if ($char eq ',' && !$quoted) { $count++; next; } if ($char eq q/"/) { unless ( $quoted && s/^\"// ) { $quoted = 1 - $quoted; nex +t; } } $array[$count] .= $char; } return @array; } for my $line (<DATA>) { print "\nLine: $line"; print "$_\n" for csv_split $line; } __DATA__ Simple cells,test,test Cells with commas,"test 1a,1b","test, 2a 2b" Cells with quotes,"test ""one""","""test"" two" Quotes and commas,"test ""1a,1b""","""test 2a,""2b"

Sample of using undef and EDI element delimiter to control when to parse the meat of a transaction:

sub handle_edi { my ($bank_name, $bu, $amt); local $/ = shift; open INFILE, shift; my $dlm = undef; while (<INFILE>) { if ( /^ST(\W)820/ ) { $dlm = $1; } elsif ( $dlm && /^SE$dlm/ ) { undef $dlm; } elsif ( $dlm && /^BPR$dlm/ ) { my @sg=split "\\$dlm"; $bank_name = $who{$sg[7]} || 'undefined'; $amt = $sg[2]; $amt =~ s/\.//g; $bu = &getbu ($sg[9]); $buhash{$bu}{bankname}=$bank_name if !defined $buhash{$bu} +{bankname}; $buhash{$bu}{count}{edi}++; $buhash{$bu}{$amt}{edi}++; } } close INFILE; }


Template-to-HTML while avoiding existing HTML:

my %sc = qw ( \\* b / em _ u ); my %counts; my %opened; sub track { $opened{$_} = 1 - ( $opened{$_} || 0 ); return "<$sc{$_}>" if --$counts{$_} > 0 && $opened{$_}; return "</$sc{$_}>" if ! $opened{$_}; $_; } sub reg_fix { my $line=shift; %counts = (); %opened = (); $line =~ s/<([^>]*<)/\&lt;$1/g; # Replace unmatched < with &lt; my @elems = split '(<.*?>)', $line; $line =~ s/<.*?>//g; $counts{$_} = @{[$line =~ m/$_/g]} for keys %sc; for my $elem ( grep !/^</, @elems ) { $elem =~ s#$_#&track#eg for + keys %sc; } join '',@elems; }

Small YYYYMMDD date formater

$YYYYMMDD = 19000100+sprintf"%02d"x3,(localtime)[5,4,3];

Using same idea to get a hash of dates who's values are total file sizes from files last modified on those days.

find .|perl -lne'$t{19000100+sprintf"%02d"x3,(localtime((stat)[9]))[5, +4,3]}+=-s if!-d}{print"$_ $t{$_}"for sort keys%t'

Universal ANSI X.12 data unwrapper:

#!/usr/bin/perl while(1) { if (!$ARGV || /^IEA/) { $/ = \1; 1 while ($_ = <> || last) =~ /\s/; $/ = \104; die "Interchange can't start with $_" unless ($_ .= <>) =~ /^I +SA[^\w\n]/; $/ = \(1 + y/\n//); $/ = substr( ($_ .= <>) , -1); die "Invalid terminator in $ARGV" if /\w$|\n.*\n$/ || ! s!\n|$ +/!!g; } else { last if !defined (($_ = <>) =~ s!\n|$/!!g); } print "$_\n"; }

Console Tic Tac Toe

$_="123 456 789 147,258,369,159,357";$p=X;sub d{/.{12}/s;print$&.$p;die$_ for@_} n:d;$m=<>until$m=~/\d/&&s/$&/$p/g;/$p{3}/?d$p:!/\d/?d"Tie":$p=~y/XO/OX +/;goto n
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found