Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comparing arrays

by MoodyDreams999 (Beadle)
on Apr 07, 2023 at 19:39 UTC ( [id://11151511]=perlquestion: print w/replies, xml ) Need Help??

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

I have some code that parses a excel file, basically contact info Name, middle, last, address, city, state, and phone number. Right now it works to parse through the phones and to populate zip codes, but I need to be able to populate state based off zip and I need to be able to populate both based off phone area code. Could you guys help me with this? I'm thinking I could create another file or 2 and have my code reference it for zip code reference and area code reference. How would I do that? This is what I have so far.

input from an excel
195 Rama Sabbavarapu 7370 Capri Way Apt 1 Maineville OH 43230 5138237094 DEBT Homeowner
195 Ramin Sayan 4207 Sorren Ct Columbus 3302230000 DEBT Homeowner
195 Ramona Sheehy 1077 Claudia Ave Apt H London 45342 7406044747 DEBT Homeowner
195 Randa Mikaiel 5524 Sagewood Dr Miamisburg oh 9376400007 DEBT Homeowner
<parse should do a few things first, where there's no zip, add the zip based on state, which I accomplished. Step 2 should calculate state based on zip, which will probably have to be done by connecting to my sql, I'm working on that currently, but stumbling due being a perl noob. 3rd I will either have to create a mysql table or another file to reference area codes, so if zip and state are missing it will check the first 3 numbers of the phone to populate those 2. This should populate a log file of whats been changed something I also have struggled with to get right./p> Output:
195 Rama Sabbavarapu 7370 Capri Way Apt 1 Maineville OH 43230 5138237094 DEBT Homeowner
195 Ramin Sayan 4207 Sorren Ct Columbus OH 43230 3302230000 DEBT Homeowner
195 Ramona Sheehy 1077 Claudia Ave Apt H London OH 45342 7406044747 DEBT Homeowner
195 Randa Mikaiel 5524 Sagewood Dr Miamisburg OH 45645 9376400007 DEBT Homeowner
We get some really horrible data lists sometimes, so this would save an exponential amount of time.
use strict; use warnings; use Excel::Writer::XLSX; my $in_file = "filein.xlsx"; my $out_file = "fileout.xlsx"; # Read in all rows from existing file my $rows = read_excel($in_file); my $AL_zip = '35007'; my $AK_zip = '99501'; my $rowcount = 0; # Update the rows for my $row (@$rows) { if($row->[6] eq 'AL' && $row->[7]) { $row->[7] = $AL_zip; } elsif(length($row->[8]) > 10 || (length $row->[8]) < 10) { ($row->[0], $row->[1], $row->[3], $row->[4]) = undef; } } # add headers as first row unshift(@$rows, $headers); # Write the updated rows to new file my $col_num = scalar @$headers - 1; write_excel($out_file, $rows, $col_num); sub read_excel { my ( $file, $sheet ) = @_; $sheet ||= 0; my $parser = Spreadsheet::ParseXLSX->new(); my $workbook = $parser->parse($file); if ( not defined $workbook ) { die $parser->error; } my $worksheet = $workbook->worksheet($sheet); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my @rows; for my $row ( $row_min .. $row_max ) { my @cells; for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); if (not $cell) { push(@cells,''); next; } my $value = $cell->value(); push(@cells,$value); } push(@rows,\@cells); } return \@rows; } sub write_excel { my ( $file, $rows, $col_max ) = @_; my $workbook = Excel::Writer::XLSX->new( $file ); if ( not defined $workbook ) { die "Could not open file: $!"; } my $worksheet = $workbook->add_worksheet(); my $worksheet2 = $workbook->add_worksheet(); my $row_num = 0; for my $row ( @$rows ) { for my $col (0 .. $col_max) { $worksheet->write( $row_num, $col, $row->[$col] ); + } $row_num++; } $workbook->close(); return; }

Replies are listed 'Best First'.
Re: Comparing arrays
by kcott (Archbishop) on Apr 07, 2023 at 21:15 UTC

    G'day MoodyDreams999,

    This looks like much the same question as you asked last month: "Multiple file handles".

    You received multiple responses with options of how to do this using external files, databases, spreadsheets, maybe others — I recalled the post but only briefly scanned it again today.

    A lot of people put a lot of effort into providing you with help: you didn't reply to any of them.

    I suggest you revisit that post and the suggestions therein. Provide us with feedback regarding why some of those options were unsuitable, or problems you encountered implementing them. You should also show some common decency and thank those that attempted to help you.

    — Ken

      I will go back and reference that, another project got in the way and I honestly was deep in had to scrap it. Sorry about the mess on the post was struggling posting the excel input and output in correct format. Thank you
        "... struggling posting the excel input and output in correct format."

        The following should help you in this regard:

        I'm currently looking at the third version of your post. The block of text starting "input from an excel 195 ..." was originally absent, then appeared near the end, then moved to near the top. It's absolutely fine to edit your post; however, you need to provide advice on the modifications you have made — see "How do I change/delete my post?" for further discussion about this.

        By the way, that block of text is virtually unintelligible as it stands. It's good that you are working on it. I'm aware that the "Edit" functionality is not as good as you get when first adding a post; especially the lack of a "Preview" option. I suggest you use your Private Scratchpad to get the formatting correct; then do a single update. In case you don't know, from your home node, it's the second "Edit" link in "Scratchpad: View, Edit, (Private: View, Edit)".

        — Ken

        From me:

        "... you didn't reply to any of them."

        From you:

        "I will go back and reference that, ..."

        I see you've added six replies. Thankyou for dealing with that in a timely fashion.

        — Ken

Re: Comparing arrays
by Anonymous Monk on Apr 07, 2023 at 19:50 UTC
    I would help us greatly if you provided us with a Short, Self-Contained, Correct Example, concentrating on the essential problem, showing input and expected output and avoiding unnecessary code, like e.g. to parse Excel.
      Thank you, ill work on getting more short and precise once I finish this other project, I actually figured out a work around with out connecting to the database. It works well since the typical data I'm dealing with is pretty small scale 10k-100k worth of lines.
Re: Comparing arrays
by etj (Deacon) on Apr 13, 2023 at 23:43 UTC
    If that is real people's data, which it looks like, you need to anonymise it right now.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11151511]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found