Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:35:45+00:00 2026-05-28T20:35:45+00:00

I’m using DBI to connect to Sybase to grab records in a hash_ref element.

  • 0

I’m using DBI to connect to Sybase to grab records in a hash_ref element. The DBI::Sybase driver has a nasty habit of returning records with trailing characters, specifically \x00 in my case. I’m trying to write a function to clean this up for all elements in the hashref, the code I have below does the trick, but I can’t find a way to make it leaner, and I know there is away to do this better:

#!/usr/bin/perl

my $dbh = DBI->connect('dbi:Sybase:...');

my $sql = qq {SELECT * FROM table WHERE age > 18;};
my $qry = $dbh->selectall_hashref($sql, 'Name');

        foreach my $val(values %$qry) {
                $qry->{$val} =~ s/\x00//g;
        }
        foreach my $key(keys %$qry) {
                $qry->{$key} =~ s/\x00//g;
                foreach my $val1(keys %{$qry->{$key}}) {
                        $qry->{$key}->{$val1} =~ s/\x00//g;
                }
                foreach my $key1(keys %{$qry->{$key}}) {
                        $qry->{$key}->{$key1} =~ s/\x00//g;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:35:46+00:00Added an answer on May 28, 2026 at 8:35 pm

    While I think that a regex substitution is not exactly an ideal solution (seems like it should be fixed properly instead), here’s a handy way to solve it with chomp.

    use Data::Dumper;
    
    my %a = (
        foo => {
            a => "foo\x00",
            b => "foo\x00"
        },
        bar => {
            c => "foo\x00",
            d => "foo\x00"
        },
        baz => {
            a => "foo\x00",
            a => "foo\x00"
        }
    );
    $Data::Dumper::Useqq=1;
    print Dumper \%a;
    {
        local $/ = "\x00";
        chomp %$_ for values %a;
    }
    print Dumper \%a;
    

    chomp will remove a single trailing value equal to whatever the input record separator $/ is set to. When used on a hash, it will chomp the values.

    As you will note, we do not need to use the values directly, as they are aliased. Note also the use of a block around the local $/ statement to restrict its scope.

    For a more manageable solution, it’s probably best to make a subroutine, called recursively. I used chomp again here, but you can just as easily skip that and use s/\x00//g. Or tr/\x00//d, which basically does the same thing. chomp is only safer in that it only removes characters from the end of the string, like s/\x00$// would.

    strip_null(\%a);
    print Dumper \%a;
    
    sub strip_null {
        local $/ = "\x00";
        my $ref = shift;
        for (values %$ref) {
            if (ref eq 'HASH') {
                strip_null($_); # recursive strip
            } else {
                chomp;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.